if(isset($_POST['id'])) {
$id=$_POST['id'];
echo $id;
$busnumber=$_POST['busnumber'];
$status=$_POST['status'];
$startpoint=$_POST['startpoint'];
$stop1=$_POST['stop1'];
$stop2=$_POST['stop2'];
}
I want to create dynamic $stop2=$_POST['stop2']; and mysql query:
$sql = mysql_query("
UPDATE fromto
SET busNumber='$busnumber', status='$status', startPoint='$startpoint',
stop1='$stop1', stop2='$stop2', stop3='$stop3', stop4='$stop4',
stop5='$stop5', stop6='$stop6', stop7='$stop7'...............
WHERE id=$id
");
You need to evaluate all variables to check which are valid SQL fields. In the most general case you might have POST names and SQL names not alike.
At that point you do not even need to set the variables.
I added an
escapecheck because, were someone to send you, say,as the value of
busNumber, your query would become:and since “–” starts a comment, MySQL would see:
which would then bork the
busNumbercolumn in the whole table. You so don’t want this to happen. PDO is a good alternative tomysql_*functions that would help prevent such problems.Anyway, you use
mysql_*, so:Moreover, it would be probably useful (performance-wise and maintenance-wise) to normalize the schema by removing the
stop*columns and putting them in another table:or even
so that if you e.g. renamed a stop from “Street 1 and Street 2” to “Streets 1-2”, the rename would affect automatically all buses with a stop there, and so on.