javascript - php for loop issue for writting to SQL database
Get the solution ↓↓↓Can anyone help with the following issue?
The full code is here:- https://affectivevideo.co.uk/code/
Application - https://affectivevideo.co.uk/avs/
The clips are selected randomly from a dataset of 100 so it only lists 10 at random into a slider with a FilmID.
$sql = "SELECT * FROM Films ORDER BY RAND() LIMIT 10";
We need to pull in the following results for each clip into the database.
('$_POST[$avalue]', '$_POST[$vvalue]', '$_POST[$filmid]', '$_POST[parnum]' )";
This is the current code I just don't know how to get the filmID in to enable writing it to the database.
$servername = "shareddb-u.hosting.stackcp.net";
$username = "cl57-avs-31333347a0";
$password = "#";
$dbname = "cl57-avs-31333347a0";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Users (PARNUM, EMAIL)
VALUES ('$_POST[parnum]', '$_POST[email]')";
if ($conn->query($sql) === TRUE) {
echo "User record created successfully <br>";
} else {
echo "User record error: " . $sql . "<br>" . $conn->error;
}
====WORKS to here===
$avalue = "Avalue";
$avalue = mysql_real_escape_string($avalue);
$vvalue = "Vvalue";
$vvalue = mysql_real_escape_string($vvalue);
$filmid = "film";
$filmid = mysql_real_escape_string($filmid);
for ($x = 1; $x <= 40; $x++) {
$raval = "$avalue$x";
$rvval = "$vvalue$x";
$rfilmid = "$filmid$x";
$sqlRes = "INSERT INTO Results ( AVALUE, VVALUE, FILMID, PARNUM) VALUES ('$_POST[$raval]', '$_POST[$rvval]', '$_POST[$rfilmid]', '$_POST[parnum]' )";
if ($conn->query($sqlRes) === TRUE) {
echo "AV New record created successfully";
echo "$raval , $rvval , $rfilmid" . "<br />";
} else {
echo "AV Error: " . $sqlRes . "</br>" . $conn->error;
}
}
print_r($_POST);
$conn->close();
?>
Old Code PHP 5.6. This worked when it was a random list of 40 videos every time. Now it is 10 random videos from 100. I think the issue is I don't have anything that I can match up the random clip number. 3 are currently being used for testing.
Results POST readout :-
New records created successfully
Array ( [email] => [email protected] [parnum] => 3 [Avalue38] => 3 [Vvalue38] => 3 [Avalue7] => 1 [Vvalue7] => 1 [Avalue85] => 8 [Vvalue85] => 8 )
Update 17/07/2020 - based on waterloomatt comment
If I set the $_POST[Avalue??] in the example to one of the clip numbers "97" its posted the results to the DB. The issue I can't solve is how I get it to know what clips are being called and or run thought a counter of the optional 100 to narrow it down.
$stmt = $conn->prepare("INSERT INTO Results (AVALUE, VVALUE, FILMID, PARNUM) VALUES (?, ?, ?,?)");
$stmt->bind_param("ssss", $avalue, $vvalue, $filmid, $parnum);
$avalue = "$_POST[Avalue97]";
$vvalue = "$_POST[Vvalue97]";
$filmid = "$_POST[Clip97]";
$parnum = "$_POST[parnum]";
$stmt->execute();
echo "New records created successfully";
User record created successfully New records created successfully Array ( [email] => [email protected] [parnum] => 1 [Avalue97] => 9 [Vvalue97] => 9 [Avalue13] => 1 [Vvalue13] => 1 )
This writes one record for clip 97 but this is because I have manually assigned the number to the code.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: dompdf image not found or type unknown
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.