我使用此代码在我的数据库中创建一个表
$con = mysqli_connect($host,$username,$password,$db);
$sql = "CREATE TABLE My_Table(";
for($i = 1; $i<=50 ; $i++) {
if($i!=50)
$sql .= "id_".$i." INT(30) NOT NULL DEFAULT '0',";
else
$sql .= "id_".$i." INT(30) NOT NULL DEFAULT '0')";
}
if (mysqli_query($con,$sql)) {
echo "Done";
}
现在我有$var = 10;
,我希望得到例如id_10
并将id_10
的值更改为id_10+=$var
。
我是PHP / MySQL的初学者。提前谢谢。
答案 0 :(得分:1)
选择
my $stmt = $con->prepare($con,"SELECT id_".$var." FROM My_table WHERE ...");
$stmt->bind_param($stmt, /* something here which depends on what you need for your condition */ );
$stmt->execute();
更新
my $stmt = $con->prepare($con,"UPDATE My_table SET id_".$var."=id_".$var."+? FROM My_table WHERE ...");
$stmt->bind_param($stmt,"i",$var /* to be changed depending on condition */);
$stmt->execute();
请注意,必须确保$ i非常严格地检查它是否来自浏览器($_GET
,$_POST
,$_COOKIE
... )。否则,这会打开可怕的SQL注入错误的大门。
但请注意上面的评论,我不认为这是一个很好的架构。