php - 在 sql 中插入和更新文本

标签 php mysql sql-update

除了将新文本更新到数据库之外,我有一个页面似乎工作正常。每次我点击提交时,它都会返回到原始数据。请让我知道要成功查询旧数据并更新新数据还缺少什么。

if (isset($_POST["submit"]) && $_POST["submit"] == "Update Load")
{
    for ($count = 1; $count <= 6; $count++)
    {
        $fields[$count] = "";
        if (isset($_POST["field" . $count . ""]))
        {
            $fields[$count] = trim($_POST["field" . $count . ""]);
            //echo $fields[$count] . "<br />";
        }
    }

    $con = mysql_connect("", "", "");
    mysql_select_db("", $con);

    $carriername = mysql_real_escape_string($_POST['carriername']);
    $contact = mysql_real_escape_string($_POST['contact']);
    $phone = mysql_real_escape_string($_POST['phone']);
    $rating = mysql_real_escape_string($_POST['rating']);
    $info = mysql_real_escape_string($_POST['info']);



    $insert = "UPDATE carrierinfo Set `carriername` = '$carriername', `contact` = '$contact', `phone` = '$phone', `rating` = '$rating', `info` = '$info'  WHERE `id` = '$id';";
    mysql_query($insert) or die(mysql_error());

    $select = "SELECT `carriername` ,`contact` ,`phone` ,`rating` ,`info` ,`id` FROM `carrierinfo` ORDER BY `carriername` DESC;";
    $result = mysql_query($select) or die(mysql_error());
}
 if ($rating == "")
    {
    $rating = "3";
    }
if (isset($_GET["id"]))
{
    $con = mysql_connect("", "", "");
    mysql_select_db("", $con);
    $id = mysql_real_escape_string($_GET["id"]);
    $select = "SELECT * FROM `carrierinfo` WHERE `id` = '$id'";
    $result = mysql_query($select) or die(mysql_error());
    $fields = mysql_fetch_array($result, MYSQL_BOTH);
    mysql_close($con);
}
else
{
    header("Location:board.php");
}
?>
</script>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<div
 style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
  <div
  style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
  <table style="font-weight: normal; width: 100%; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
   <table
 style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
    <tbody>
      <tr>
              <td style="width: 35%;">Carrier Name:</td><td><input id="carriername" name="carriername" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[carriername]; ?>">
                    </tr>
        <tr>
              <td style="width: 35%;">Contact:</td><td><input id="contact" name="contact" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[contact]; ?>">
                    </tr>
        <tr>
              <td style="width: 35%;">Phone:</td><td><input id="phone" name="phone" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[phone]; ?>">
                    </tr>
                    <tr>
      <td style="width: 10%;">Carrier Rating:</td><td>
              <select id="rating" name="rating">
              <option value=""></option>
              <option <?php if($fields[rating] == "1") echo "selected"; ?> value="1">1</option>
              <option <?php if($fields[rating] == "2") echo "selected"; ?> value="2">2</option>
              <option <?php if($fields[rating] == "3") echo "selected"; ?> value="3">3</option>
              <option <?php if($fields[rating] == "4") echo "selected"; ?> value="4">4</option>
              <option <?php if($fields[rating] == "5") echo "selected"; ?> value="5">5</option>
              </select>
              </td>
        </tr>
        <tr>
      <td style="width: 10%;">Carrier Info:</td><td style="text-align: center;" colspan="2"><textarea name="info" maxlength="65535"  style="width: 100%; height: 4em;"><?php echo $fields[info]; ?></textarea></td>
      </tr>
     </tbody>
  </table>
  <p style="text-align: center;"><input name="submit" value="Update"
 class="submit" type="submit"></p>
  </div>
</form>
<input type="button" onclick="window.location.href='test3.php';" value="Back" />
</div>
<p style="margin-bottom: -20px;">&nbsp;</p>
</body>

最佳答案

$insert = "UPDATE carrierinfo Set `carriername` = '$carriername', `contact` = '$contact', `phone` = '$phone', `rating` = '$rating', `info` = '$info'  **WHERE `id` = '$id';**";

在运行 UPDATE 查询之前,您尚未声明 $id 应该是什么,因此不会更新任何内容。

关于php - 在 sql 中插入和更新文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316791/

相关文章:

php - MySQL链接(mysql_connect)是否可以存储在Memcache中重复使用?

php - 为什么 foreach 将 refcount 增加 2 而不是 1?

mysql - Ruby on Rails where 子句找不到正确的记录

php - 将数据从mysql插入到phpexcel的数组中

php - 我如何使用 SQL 和 PHP 更新这些表条目?

mysql - 如何在 r 中执行 MySQL 更新查询

php - 在集合中搜索 laravel

javascript - 如何创建类似 jQuery Accordion 的东西?

mysql - 使用 2 个 WHERE 子句

mysql - 使用 mysql-python 将值插入 SQL 列