试图更新mysql数据库中的记录(php)

时间:2014-03-15 02:59:26

标签: php mysql mysqli sql-update

我一直在浏览StackOverflow和其他网站寻找其他方法来做到这一点,我尝试过的任何事情似乎都没有效果。

editscript.php似乎停在echo "making connection<br>";并且数据库没有更新,我不能为什么做出正面或反面。

我真的很欣赏一些关于我哪里出错的建议。

干杯!

&LT;&LT;&GT;&GT;

<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>

&LT;&LT;&GT;&GT;

<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>

1 个答案:

答案 0 :(得分: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);