保存更改按钮不起作用

时间:2014-02-19 15:44:02

标签: php mysql

我还在制作订单,我一直致力于“保存更改功能”。 我的意思是,如果人们对表进行更改,他们可以单击一个按钮,它将更新数据库(我从中获取数据)。

Screenshot

“Bestelling toevoegen”是添加表格,如果我填写表格,点击“Toevoegen”,它将在上表中显示一条新记录。

问题是,我似乎无法保存“状态”的变化(Wijzigigen opslaan)。

这是完整的代码:

    $dbname = "localhost";
    $dblogin = "root";
    $dbpass = "";
    $dbtable = "bestelformulier";

    $con=mysqli_connect("$dbname","$dblogin","$dbpass","$dbtable");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM overzicht");

    echo "<form name='wijzigen' method='post'>";
    echo "<table align='center' width='700px' border='2'>
    <tr>
    <th>Ordernr</th>
    <th>Klantnaam</th>
    <th>Productnaam</th>
    <th>ProductID</th>
    <th>Status</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {

    $ordernr = $row['ordernr'];
    $klantnaam = $row['klantnaam'];
    $productnaam = $row['productnaam'];
    $productid = $row['productid'];

    echo "<tr>";
    echo "<td>" . $ordernr . "</td>";
    echo "<td width='150px'>" . $klantnaam . "</td>";
    echo "<td width='200px'>" . $productnaam . "</td>";
    echo "<td>" . $productid . "</td>";

    echo "<td><select name='status'>
      <option>" . $row['status'] . "</option>";
      if($row['status']  != "Niet besteld")
      echo "<option value='Niet besteld'>Niet besteld</option>";
      if($row['status']  != "Besteld")
      echo "<option value='Besteld'>Besteld</option>";
      if($row['status']  != "Onderweg naar hoofdlocatie")
      echo "<option value='Onderweg naar hoofdlocatie'>Onderweg naar hoofdlocatie</option>";
      if($row['status']  != "Onderweg naar vestiging")
      echo "<option value='Onderweg naar vestiging'>Onderweg naar vestiging</option>";
      if($row['status']  != "Ontvangen")
      echo "<option value='Ontvangen'>Ontvangen</option>";
    echo "</select></td>";

    echo "</tr>";

    }
    echo "<tr>";
    echo "<td></td><td></td><td></td><td></td>";
    echo "<td><input type='submit' name='wijzigen' value='Wijzigingen Opslaan'/></td>";
    echo "</tr>";
    echo "</table>";
    echo "</form>";

    $status = $_POST['status'];

    if(isset($_POST['wijzigen'])) {
    $query = mysqli_query("UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");
    }

    mysqli_close($con);

    //Table Toevoegen
    $con=mysqli_connect("$dbname","$dblogin","$dbpass","$dbtable");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    echo "<br/><br/><br/>";
    echo "<h5>Bestelling Toevoegen</h5>";
    echo "<form name='toevoegen' method='post'>";
    echo "<table width='700px' border='1'>

    <tr>
    <th>klantnaam</th>
        <td><input type='text' name='klantnaam'/></td>
    </tr>

    <tr>
      <th>Productnaam</th>
        <td><input type='text' name='productnaam'/></td>
    </tr>

    <tr>
      <th>productid</th>
        <td><input type='text' name='productid'/></td>
    </tr>



    <tr>
       <th>Status</th>
       <td>
         <select name='statusinfo'>
           <option value='Niet besteld'>Niet besteld</option>
           <option value='Besteld'>Besteld</option>
           <option value='Onderweg naar hoofdlocatie'>Onderweg naar hoofdlocatie</option>
           <option value='Onderweg naar vestiging'>Onderweg naar vestiging</option>
           <option value='Ontvangen'>Ontvangen</option>
         <select>
       </td>

    </tr>

    <tr>
        <td></td>
        <td><input type='submit' name='toevoegen' value='Toevoegen'/></td>
    </tr>";


    echo "</table>";


    $klantnaam = $_POST['klantnaam'];
    $productnaam = $_POST['productnaam'];
    $productid = $_POST['productid'];
    $statusinfo = $_POST['statusinfo'];

    if(isset($_POST['toevoegen'])) {

    $query = mysqli_query($con,"INSERT INTO overzicht (klantnaam, productnaam, productid, status)
    VALUES ('$klantnaam', '$productnaam', '$productid', '$statusinfo')");

    $current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header ('Location: ' . $current_url);
    exit ();
    }
    echo "</form>";
    mysqli_close($con);

2 个答案:

答案 0 :(得分:1)

我相信您遇到了问题,因为您输出的多个select元素都名为status。尝试更改此行:

echo "<td><select name='status'>

为:

echo "<td><select name='status[$ordernr]'>

然后您将拥有一个状态数组,其中键是订单号。然后改变这个:

$status = $_POST['status'];

if(isset($_POST['wijzigen'])) {
$query = mysqli_query("UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");
}

为:

$statuses = $_POST['status'];

if(isset($_POST['wijzigen'])) {
    foreach($statuses as $ordernr => $status)
    {
        if($status != "")
            $query = mysqli_query($con, "UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");
    }
}

答案 1 :(得分:0)

这一行:

$query = mysqli_query("UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");

您的$ordernr为空。如果要更新现有记录,则必须通过表单上的GET或POST传递ordernr。

但是,您的表单设计只允许同时更新一个订单状态。您应该将所有订单及其状态作为数组传递,或者通过AJAX传递。

另外,请阅读有关SQL注入的内容。如果此代码生效,您的数据库将被黑客攻击,保证。