PHP / MYSQL: Database empties when I use a variable in the WHERE condition of the last mysql_query
- by Christian Cugnet
<?php
require 'connect.php';
$search = $_POST["search"];
These two queries work fine. So I used their format for the one below.
$result = mysql_query("SELECT * FROM `subjects` WHERE $search = `student_id`");
$result2 = mysql_query("SELECT * FROM `grades` WHERE $search = `student_id`");
while($row = mysql_fetch_array($result)) {
$row2 = mysql_fetch_array($result2);
echo"<table border='1'>";
echo "<tr>";
echo    "<th>Subjects:</th>";
echo    "<th>Current Mark:</th>";
echo    "<th>Edit Mark:</th>";
echo"</tr>";
echo"<tr>";
echo "<td>". $row['c1'] ."</td>";
echo "<td>". $row2['m1'] ."</td>";
echo "<td><input type='text' name='m1'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c2'] ."</td>";
echo "<td>". $row2['m2'] ."</td>";
echo "<td><input type='text' name='m2'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c3'] ."</td>";
echo "<td>". $row2['m3'] ."</td>";
echo "<td><input type='text' name='m3'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c4'] ."</td>";
echo "<td>". $row2['m4'] ."</td>";
echo "<td><input type='text' name='m4'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c5'] ."</td>";
echo "<td>". $row2['m5'] ."</td>";
echo "<td><input type='text' name='m5'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c6'] ."</td>";
echo "<td>". $row2['m6'] ."</td>";
echo "<td><input type='text' name='m6'></td>";
echo  "</tr>";
echo  "<tr>";
echo    "<td>". $row['c7'] ."</td>";
echo "<td>". $row2['m7'] ."</td>";
echo "<td><input type='text' name='m7'></td>";
echo  "</tr>";
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
}
 $M1 = $_POST["m1"];
 $M2 = $_POST["m2"];
 $M3 = $_POST["m3"];
 $M4 = $_POST["m4"];
 $M5 = $_POST["m5"];
 $M6 = $_POST["m6"];
 $M7 = $_POST["m7"];
It works if I put numbers e.x. 11111
Otherwise it just enters blank spaces into the table.
I've tried '".$search."'
I've tried ".$search."
mysql_query("UPDATE grades SET m1 = '$M1', m2 = '$M2',m3 = '$M3',m4 = '$M4',m5 =    '$M5',m6 = '$M6',m7 = '$M7' WHERE $search = `student_id`");
?>
Table
+------------+---+---+---+---+---+---+---+
|student_id|m1|m2|m3|m4|m5|m6|m7|
+------------+---+---+---+---+---+---+---+
===Database d1
== Table structure for table grades
|------
|Column|Type|Null|Default
|------
|//student_id//|int(5)|No|
|m1|text|No|
|m2|text|No|
|m3|text|No|
|m4|text|No|
|m5|text|No|
|m6|text|No|
|m7|text|No|
== Dumping data for table grades
|11111| | | | | | | 
|11112|fg|fd|f|f|fd|f|f
===Database d1
== Table structure for table subjects
|------
|Column|Type|Null|Default
|------
|//student_id//|int(11)|No|
|c1|text|No|
|c2|text|No|
|c3|text|No|
|c4|text|No|
|c5|text|No|
|c6|text|No|
|c7|text|No|
== Dumping data for table subjects
|11111|English|Math|Science|Sport|IT|Art|History
|11112|grdgg|vsbvbbb|bdbbrfd|bdbrb|dbrbfbf|fbdfbdbf|dbfbdfb