更新查询 PHP MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8893551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:43:56  来源:igfitidea点击:

Update query PHP MySQL

phpmysqldatabasephpmyadminblogs

提问by davidethell

Can anybody help me understand why this update query isn't updating the fields in my database? I have this in my php page to retrieve the current values from the database:

有人能帮我理解为什么这个更新查询没有更新我数据库中的字段吗?我在我的 php 页面中有这个来从数据库中检索当前值:

<?php

  $query = mysql_query ("SELECT * FROM blogEntry WHERE username = 'bobjones' ORDER BY id DESC");

  while ($row = mysql_fetch_array ($query)) 
  {
      $id = $row['id']; 
      $username = $row['username'];
      $title = $row['title'];
      $date = $row['date'];
      $category = $row['category'];
      $content = $row['content'];


    ?>

Here i my HTML Form:

这是我的 HTML 表单:

<form method="post" action="editblogscript.php">
ID: <input type="text" name="id" value="<?php echo $id; ?>" /><br />
Username: <input type="text" name="username" value="<?php echo $_SESSION['username']; ?>" /><br />
Title: <input type="text" name="udtitle" value="<?php echo $title; ?>"/><br />
Date: <input type="text" name="date" value="<?php echo $date; ?>"/><br />
Message: <textarea name = "udcontent" cols="45" rows="5"><?php echo $content; ?></textarea><br />
<input type= "submit" name = "edit" value="Edit!">
</form>

and here is my 'editblogscript':

这是我的“editblogscript”:

<?php

mysql_connect ("localhost", "root", "");
mysql_select_db("blogass");

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

    $id = $_POST['id'];
    $udtitle = $_POST['udtitle'];
    $udcontent = $_POST['udcontent'];


    mysql_query("UPDATE blogEntry SET content = $udcontent, title = $udtitle WHERE id = $id");
}

header( 'Location: index.php' ) ;





?>

I don't understand why it doesn't work.

我不明白为什么它不起作用。

回答by davidethell

You have to have single quotes around any VARCHAR content in your queries. So your update query should be:

您必须在查询中的任何 VARCHAR 内容周围使用单引号。所以你的更新查询应该是:

mysql_query("UPDATE blogEntry SET content = '$udcontent', title = '$udtitle' WHERE id = $id");

Also, it is bad form to update your database directly with the content from a POST. You should sanitize your incoming data with the mysql_real_escape_string function.

此外,直接使用 POST 中的内容更新数据库也是一种糟糕的方式。您应该使用 mysql_real_escape_string 函数清理传入的数据。

回答by MrKiane

Without knowing what the actual error you are getting is I would guess it is missing quotes. try the following:

在不知道您得到的实际错误是什么的情况下,我猜它缺少引号。尝试以下操作:

mysql_query("UPDATE blogEntry SET content = '$udcontent', title = '$udtitle' WHERE id = '$id'")

回答by Rabesh Lal Shrestha

Need to add quote for that need to use dot operator:

需要为需要使用点运算符添加引号:

mysql_query("UPDATE blogEntry SET content = '".$udcontent."', title = '".$udtitle."' WHERE id = '".$id."'");

回答by sudhakar

Here i updated two variables and present date and time

在这里我更新了两个变量并显示日期和时间

$id = "1";
$title = "phpmyadmin";

$id = "1";
$title = "phpmyadmin";

 $sql=  mysql_query("UPDATE table_name SET id ='".$id."', title = '".$title."',now() WHERE id = '".$id."' ");

now() function update current date and time.

now() 函数更新当前日期和时间。

note: For update query we have define the particular id otherwise it update whole table defaulty

回答by pduersteler

First, you should define "doesn't work".
Second, I assume that your table field 'content' is varchar/text, so you need to enclose it in quotes. content = '{$content}'
And last but not least: use echo mysql_error()directly after a query to debug.

首先,您应该定义“不起作用”。
其次,我假设您的表字段“内容”是 varchar/text,因此您需要将其括在引号中。content = '{$content}'
最后但并非最不重要的是:echo mysql_error()在查询后直接使用以进行调试。

回答by Muhammad Anees

I would guess it is missing quotes. Try the following: I hope It will work properly

我猜它缺少引号。尝试以下操作:我希望它能正常工作

$sql= mysql_query("UPDATE table_name SET id ='".$u_id."', name = '".$u_name."',now() WHERE id = '".$u_id."' "); //linebreak

$sql= mysql_query("UPDATE table_name SET id ='".$u_id."', name = '".$u_name."',now() WHERE id = '".$u_id."'"); //越线

回答by fefe

you must write single quotes then double quotes then dot before name of field and after like that

您必须先写单引号,然后是双引号,然后在字段名称之前加上点,然后像这样

mysql_query("UPDATE blogEntry SET content ='".$udcontent."', title = '".$udtitle."' WHERE id = '".$id."' ");

回答by A.A Noman

Update a row or column of a table

更新表格的一行或一列

$update = "UPDATE daily_patients SET queue_status = 'pending' WHERE doctor_id = $room_no and serial_number= $serial_num";

if ($con->query($update) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $con->error;
}

回答by A.A Noman

<?php
require 'db_config.php';


  $id  = $_POST["id"];
  $post = $_POST;

  $sql = "UPDATE items SET title = '".$post['title']."'

    ,description = '".$post['description']."' 

    WHERE id = '".$id."'";

  $result = $mysqli->query($sql);


  $sql = "SELECT * FROM items WHERE id = '".$id."'"; 

  $result = $mysqli->query($sql);

  $data = $result->fetch_assoc();


echo json_encode($data);
?>

回答by Vidya

Try like this in sql query, It will work fine.

在 sql 查询中尝试这样,它会正常工作。

$sql="UPDATE create_test set url= '$_POST[url]' WHERE test_name='$test_name';";

If you have to update multiple columns, Use like this,

如果您必须更新多个列,请像这样使用,

$sql="UPDATE create_test set `url`= '$_POST[url]',`platform`='$_POST[platform]' WHERE test_name='$test_name';";