[英]php update dynamic sql rows through form
My goal is to retrieve a dynamic SQL list of rows and edit every item in the list within the same page using POST (no multiple changes at once required). 我的目标是检索动态的SQL行列表,并使用POST编辑同一页面内列表中的每个项目(一次无需进行任何更改)。 Can this be achieved within the same page? 能否在同一页面上实现? I made the script below which displays everything the way i want to, however the switch statement doesn't seem to be fit for purpouse. 我在下面创建了脚本,该脚本以我想要的方式显示了所有内容,但是switch语句似乎不适合使用。
I am aware this code might not be best practice (not to mention secure). 我知道此代码可能不是最佳做法(更不用说安全了)。 Sorry for being inflexible, but i'm not allowed to use anything other then PHP/HTML 抱歉,您的代码不够灵活,但除PHP / HTML之外,我不允许使用其他任何内容
Code: 码:
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
} else {
echo "0 resultaten";
}
switch(!empty($_POST))
{
case !empty($_POST[$row["voornaam"]]):
// update SQL query 'firstname' here
break;
}
$conn->close();
?>
In your switch you're sending !empty($_POST)
so boolean (true or false) so unique cases would be: True or False... 在您的开关中,您将发送!empty($_POST)
如此的布尔值(true或false),因此唯一的情况将是:True或False ...
It's better if you change your switch to: 最好将开关更改为:
if(isset($_POST[$row["voornaam"]]) && !empty($_POST[$row["voornaam"]])){
// update SQL query 'firstname' here
}
<?php
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
}
else
{
echo "0 resultaten";
}
if( isset( $_POST[$row["voornaam"]] ) && !empty( $_POST[$row["voornaam"]] ) )
{
// update SQL query 'firstname' here
}
$conn->close();
?>
By this way you actualy see what you are editing and it's more clean. 通过这种方式,您实际上可以看到自己正在编辑的内容,并且更加干净。
// PDO DB conecction (secure way)
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){ //to handle connection error
echo "Connection error: " . $exception->getMessage();
}
if isset($_POST["voornaam"]){
$stmt = $con->prepare(" QUERY");
$stmt->execute();
echo "YEAH you did it";
} else {
stmt = $con->prepare(" QUERY");
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $rs)
{
$voornaam = $rs["voornaam"]
$achternaam = $rs["achternaam"]
$omschrijving = $rs["omschrijving"]
}
}
// Just an example
echo '<form action= "" method="post"> <input type="text" name="voornaam" value="'. $voornaam .'" style="width:100px"><input name="submit" type ="submit" value="Submit"><br/><br/>';
Value is also the input so you can have only one Submit button at the end cause all other data will be overwritten by itself. 值也是输入,因此最后只能有一个“提交”按钮,因为所有其他数据将被其自身覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.