更新多个MySql条目

时间:2014-07-03 15:45:59

标签: php mysql

我一直致力于为公司创建内部网站。到目前为止,我还没有遇到过很多问题。我已经能够检索并将数据插入到我的数据库中,但是由于某种原因,当我尝试更新条目时,由于某些奇怪的原因,无法选择数据库。到目前为止,我附上了我的代码副本。我不知道自己错过了什么。谢谢!

这是我在数据库中查找信息的代码:

   <?php
session_start();

$transport = mysqli_connect("localhost", "user", "pw", "db_name");

if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

<?php

$raw_date = $_POST['appt_date'];
$date = date("Y-m-d", strtotime($raw_date));


if ($raw_date == '') {
    echo "Please go back and pick a date";
    exit;
}

$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";

$result = mysqli_query($transport, $sql);

$i=0;

echo "<h2 align='center'>Schedule for $raw_date</h2>";

echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th></tr>";

echo "<form name='update_times' method='post' action='depart.php'>\n";
while($row = mysqli_fetch_array($result))
  {
  echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . "";
  echo "<tr>";

  echo "<td align='center'>" . $row['r_name'] . "</td>";
  echo "<td align='center'>" . $row['room'] . "</td>";
  echo "<td align='center'>" . date("g:i A", strtotime($row['appttime'])) . "</td>";  
  echo "<td align='center'>" . $row['apptphone'] . "</td>";
  echo "<td align='center'>" . $row['l_name'] . "</td>";
  echo "<td align='center'>" . $row['address'] . "</td>";
  echo "<td align='center'>" . $row['city'] . "</td>";
  echo "<td align='center'>" . $row['zip'] . "</td>";
  echo "<td align='center'>" . $row['buscar'] . "</td>";
  echo "<td align='center'>" . $row['escort_name'] . "</td>";
  echo "<td align='center'>" . $row['transfer'] . "</td>";
  echo "<td align='center'>" . $row['comments'] . "</td>";
  echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
  echo "</tr>";
  ++$i;  
  }

echo "<input type='submit' value='Set Depart Times'>";
echo "</form>";
echo "</table>";

$_SESSION['sessionVar'] = $raw_date;


?>

这是更新代码:

<?php
session_start();

$transport = mysqli_connect('localhost', 'user', 'pw', 'db_name');

if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

<?php
$size = count($_POST['out']);

$i=0;
while ($i < $size)
{
    $departing = $_POST['out'][$i];
    $departing = date("H:i:s:u",strtotime($departing));
    $id = $_POST['id'][$i];

    $sql = "UPDATE transport.appointments SET depart = $departing WHERE id = $id";
    mysqli_query($transport, $sql) or die ("Error in query: $sql");
    echo "Depart times updated!";
    ++$i;

}

mysql_close($transport);
?>

由于某种原因,更新代码不想选择我的数据库。再次感谢你!

1 个答案:

答案 0 :(得分:0)

MySQL UPDATE语法是:

UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;

您似乎已尝试在UPDATE查询中引用您的数据库和表:transport.appointments。我可能错了,但我在互联网上找不到任何说法是有效的语法。

尝试引用该表:

$sql = "UPDATE appointments SET depart = $departing WHERE id = $id";