我能在PHP中做点什么吗?:
for($i=0; $i <= 5; $i++){
$anyway = $_POST['smth'].$i;
}
如何在Mysql插入函数中使用它。
$sql = "INSERT INTO try (column_1) VALUES ('".$anyway."')";
答案 0 :(得分:1)
首先,我建议在变量中使用$ _POST ['smth'],然后再在for循环中使用它。
$smth = $_POST['smth']; //remember to sanitize strings taken from user input
$anyway = ""; // Declare $anyway before the for loop to avoid overwriting it
for($i=0; $i <= 5; $i++){
$anyway = $smth.$i;
}
然后,您需要设置并测试数据库连接:
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
最后,准备,绑定并执行您的查询:
$stmt = $mysqli->prepare("INSERT INTO (column_1) VALUES (?)");
$stmt->bind_param("s", $anyway);
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}