【问题标题】:trying to update records in mysql database (php)尝试更新mysql数据库中的记录(php)
【发布时间】:2014-04-20 13:40:47
【问题描述】:

我一直在浏览 StackOverflow 和其他网站,寻找其他方法来执行此操作,但我尝试过的任何方法似乎都没有奏效。

editscript.php 似乎停在echo "making connection<br>"; 并且数据库没有更新,我不知道为什么。

我真的很感激一些关于我哪里出错的建议。

干杯!

>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Entry</title>
</head>

<body>

<?php

echo "<a href=\"index.php\">go home, you are drunk.</a>";

$id=$_GET["id"];

//db vars
$sqlhost='localhost';
$sqluser='sqluser';
$sqlpass='sqlpass';
$sqldb='riggingregister';

// Make a MySQL Connection
$con=mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM register WHERE id='$id'");

echo "<form action=\"editscript.php\" method=\"post\">";
echo "<table width=\"372\" border=\"0\" align=\"center\">";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr><td>ID</td><td>" . $row['id'] . "</td></tr>";
  echo "<input name=\"id\" type=\"hidden\" value=\"" . $row['id'] ."\" />";
  echo "<tr><td>Register</td><td><input name=\"register\" type=\"text\" value=\"". $row['register'] ."\"/></td></tr>";
  echo "<tr><td>Location</td><td><input name=\"location\" type=\"text\" value=\"". $row['location'] ."\"/></td></tr>";
  echo "<tr><td>Type</td><td><input name=\"type\" type=\"text\" value=\"". $row['type'] ."\"/></td></tr>";
  echo "<tr><td>Capacity</td><td><input name=\"capacity\" type=\"text\" value=\"". $row['capacity'] ."\"/></td></tr>";
  echo "<tr><td>Length</td><td><input name=\"length\" type=\"text\" value=\"". $row['length'] ."\"/></td></tr>";
  echo "<tr><td>Qty</td><td><input name=\"qty\" type=\"text\" value=\"". $row['qty'] ."\"/></td></tr>";
  echo "<tr><td>Serial#</td><td><input name=\"serial\" type=\"text\" value=\"". $row['serial'] ."\"/></td></tr>";
  echo "<tr><td>Certificate#</td><td><input name=\"cert\" type=\"text\" value=\"". $row['cert'] ."\"/></td></tr>";
  echo "<tr><td>Last Inspection Completed On</td><td><input name=\"lastinsp\" type=\"text\" value=\"". $row['lastinsp'] ."\"/></td></tr>";
  echo "<tr><td>Last Inspection Completed By</td><td><input name=\"inspby\" type=\"text\" value=\"". $row['inspby'] ."\"/></td></tr>";
  echo "<tr><td>Date introduced into service</td><td><input name=\"datein\" type=\"text\" value=\"". $row['datein'] ."\"/></td></tr>";
  echo "<tr><td>Date removed from service</td><td><input name=\"dateout\" type=\"text\" value=\"". $row['dateout'] ."\"/></td></tr>";
  echo "<tr><td>Notes</td><td><input name=\"notes\" type=\"text\" value=\"". $row['notes'] ."\"/></td></tr>";
  }
echo "</table>";
echo "<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Confirm Changes\" />";
echo "</form>";

mysqli_close($con);
?> 

</body>
</html>

>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Entry (Script)</title>
</head>
<body>

<?php
echo "<a href=\"index.php\">Go Home!</a><br /><hr />";

//db vars
$sqlhost='localhost';
$sqluser='riggingregister';
$sqlpass='RIGGINGregister';
$sqldb='riggingregister';

//fetch form input
$id = $_POST['id'];
$register = $_POST['register'];
$location = $_POST['location'];
$type = $_POST['type'];
$capacity = $_POST['capacity'];
$length = $_POST['length'];
$qty = $_POST['qty'];
$serial = $_POST['serial'];
$cert = $_POST['cert'];
$lastinsp = $_POST['lastinsp'];
$inspby = $_POST['inspby'];
$datein = $_POST['datein'];
$dateout = $_POST['dateout'];
$notes = $_POST['notes'];

echo "$id<br /> $register<br /> $location<br /> $type<br /> $capacity<br /> $length<br /> $qty<br /> $serial<br /> $lastinsp<br /> $inspby<br /> $datein<br /> $dateout<br /> $notes<br /><br />";

// Make a MySQL Connection
echo "making connection<br>";
$con = mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb) or die(mysql_error());
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//perform update query
$stmt = mysqli_prepare($con,
                       "UPDATE register SET 
                        register=?, 
                        location=?, 
                        type=?, 
                        capacity=?,
                        length=?,
                        qty=?,
                        serial=?,
                        cert=?,
                        lastinsp=?,
                        inspby=?,
                        datein=?,
                        dateout=?,
                        notes=?
                        WHERE id=?") or die(mysqli_error($con));
mysqli_stmt_bind_param($stmt, 'sssiiisssssssi',
                       $register, $location, $type, $capacity, $length, $qty, $serial, $cert, $lastinsp, $inspby, $datein, $dateout, $notes, $id);
mysqli_stmt_execute($stmt);
//echo "update successful! YAY!<br />";
echo "<a href=\"index.php\">Home</a>";

//close connection to db
mysqli_close();

?> 


</body>
</html>

【问题讨论】:

  • 您遇到了什么问题?
  • mysql_error() 的参数是数据库连接,而不是字符串。
  • 您还缺少mysqli_query() 的第一个参数。
  • 更新了问题,抱歉。
  • Barmar,我根据您的建议进行了更改,但它只是在“建立连接”之后回显 Can't update the records:

标签: php mysql mysqli sql-update


【解决方案1】:

您正在混合使用 mysqlimysql 函数,您不能这样做。您也没有将连接对象传递给 mysqli 函数。您应该使用参数化查询而不是变量替换。

$con = mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb) or die("Failed to connect to MySQL: " . mysqli_connect_error());

//perform update query
$stmt = mysqli_prepare($con,
                       "UPDATE register SET 
                        register=?, 
                        location=?, 
                        type=?, 
                        capacity=?,
                        length=?,
                        qty=?,
                        serial=?,
                        cert=?,
                        lastinsp=?,
                        inspby=?,
                        datein=?,
                        dateout=?,
                        notes=?
                        WHERE id=?") or die(mysqli_error($con));
mysqli_stmt_bind_param($stmt, 'sssiiisssssssi',
                       $register, $location, $type, $capacity, $length, $qty, $serial, $cert, $lastinsp, $inspby, $datein, $dateout, $notes, $id);
mysqli_stmt_execute($stmt);
//echo "update successful! YAY!<br />";
echo "<a href=\"index.php\">Home</a>";

//close connection to db
mysqli_close($con);

【讨论】:

  • 根据建议更改,结果仍与以前相同
  • 抱歉,$con 离开了 mysqli_prepare(),现在试试吧。
  • 确保您启用了错误报告——该错误应该导致打印 PHP 错误。
  • @Barmar 您的答案仍然包含or die(mysql_error()); - 第一行。
  • 这似乎有固定的东西,谢谢大家。不过,我还有一些阅读要做,以弄清楚现在到底发生了什么!
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
  • 2019-04-02
  • 2015-08-30
  • 2018-12-10
相关资源
最近更新 更多