我是php和MySQL的新手,我正在尝试建立一个小型网站作为练习。我有这个小错误,我想编辑我的数据库中的付款,代码运行,它说它成功但它没有在表中更新。我尝试使用很多不同的方法,比如在我的数据库中创建视图,但它仍然无效。 php页面应该从上一页获取user_id,这很有效,但它仍然没有更新表。
这是我的购物篮表的SQL:
CREATE TABLE IF NOT EXISTS `basket` (
`product_id` int(8) NOT NULL,
`user_id` tinyint(4) NOT NULL,
`quantity` mediumint(8) NOT NULL,
`size` varchar(150) NOT NULL,
`payment` varchar(6) NOT NULL default 'No',
`checkout` varchar(3) NOT NULL default 'No'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `basket`
--
INSERT INTO `basket` (`product_id`, `user_id`, `quantity`, `size`, `payment`, `checkout`) VALUES
(1, 1, 1, 'Small', 'No', 'Yes'),
(3, 1, 1, 'Small', 'No', 'Yes'),
(20, 1, 1, 'Small', 'No', 'Yes'),
(3, 2, 1, 'Small', 'Yes', 'Yes');
下面是我的php代码,因为我在我的数据库中使用了名为payment_details的视图:
<?php
require_once('my_connect.php');
require_once 'header.php';
$user_id=$_GET["user_id"];?>
<html>
<head></head>
<body>
<H1>Edit Payment Form</H1>
<table>
<?php
if (isset($_POST['change_payment'])):
$user_id=$_GET["user_id"];
$payment=$_POST["payment"];
$edit_query= "update basket set
payment = '$payment' where
user_id='$user_id'";
$edit_result= mysqli_query($connection, $edit_query);
if ($edit_result) :
header ('location: view_orders.php?confirm=Product item successfully updated');
else :
echo "<b>This didn`t work, error: </b>";
echo mysqli_error($connection);
endif;
endif;
$user_id=$_GET['user_id'];
$my_query="select * from basket where user_id=$user_id;";
$result= mysqli_query($connection, $my_query);
while ($myrow = mysqli_fetch_array($result)):
$user_id= $myrow['user_id'];
$payment = $myrow["payment"];
endwhile;
echo "<form method='POST' action='change_payment.php' >
<tr><td><b>Payment:</b>
<td><input type='text' name='payment' value='$payment'>
<tr><td><input type='submit' name='change_payment' value='Save Changes'>
</form>";
?>
</table>
</body>
</html>
</div>
<?php require_once('footer.php'); ?>
它没有更新我的数据库中的篮子表,我想知道为什么?请帮助我,我会很感激。
答案 0 :(得分:1)
由于我无法发表评论(缺乏声誉,我必须将此作为答案)。首先,每个mysql / i_query()应该用棍子打它,直到它成为准备好的语句。我可以看到你正在处理付款,而且你的SQLInjection易受攻击。因此,首先要了解有关SQLInjection和预处理语句的建议。关于问题,请尝试将脚本顶部的错误报告设置为:
error_reporting(E_ALL);
ini_set('display_errors', 1);
并写下之后发生的事情。还要在更新查询之前转储$ user_id并检查是否存在一些问题,因为即使数据库中不存在$ user_id,查询也会成功更新,它会更新,但查询是成功的。
答案 1 :(得分:0)
如果MySQL具有与MS SQL类似的语法,那么我能看到的唯一问题是insert语句中的表名和列名周围的撇号。对于要插入的字符串值,您应该只需要撇号。
答案 2 :(得分:0)
试试这个对我有用,你这里有错误
"select * from basket where user_id=$user_id;"
这个;是错误。并确保你的域中有user_id index.php?user_id = 1
编辑代码已更新,检查URL中是否存在user_id,否则将无法显示表单或更新脚本,因为ur表单基于user_id从数据库读取输入。
<?php
require_once 'my_connect.php';
require_once 'header.php';
?>
<html>
<head>
<title></title>
</head>
<body>
<H1>Edit Payment Form</H1>
<table>
<?php
// if submit button is pressed
if (isset($_POST['change_payment']))
{
// check if user_id is exists in a URL
if (isset($_GET['user_id']) && !empty($_GET['user_id']))
{
// now if user_id exists and is not empty make a variable and run a rest of a script
$user_id = (int)$_GET['user_id'];
// get name from a form input field payment
$payment = $_POST["payment"];
// update basket table with payment where user_id is equal to user_id from a $_GET variable
$edit_query = "UPDATE basket SET payment = '$payment' WHERE user_id = '$user_id'";
// run query
$edit_result = mysqli_query($connection, $edit_query);
// if query is valid redirect user to another location
if ($edit_result)
{
header('location: view_orders.php?confirm=Product item successfully updated');
}
else
{
// if query fails display error message
echo "<b>This didn't work, error: </b>" . mysqli_error($connection);
}
}
}
// check if user_id is exists in a URL if not dont display a form
if (isset($_GET['user_id']) && !empty($_GET['user_id']))
{
// now if user_id exists and is not empty make a variable and run a rest of a script
$user_id = (int)$_GET['user_id'];
// select all data from basket table where user_id is equal to user_id from a $_GET variable
$my_query = "SELECT * FROM basket WHERE user_id = '$user_id'";
// run query
$result = mysqli_query($connection, $my_query);
// get an array with all data from basket table where user_id is equal to user_id from $_GET variable
$myrow = mysqli_fetch_array($result);
// show a form and set value to $myrow['payment'] where payment is equal to payment data where user_id is equal to user_id from a $_GET variable
echo '
<form method="POST" action="">
<b>Payment:</b>
<input type="text" name="payment" maxlength="6" value="'.$myrow['payment'].'">
<input type="submit" name="change_payment" value="Save Changes">
</form>
';
}
?>
</table>
</body>
</html>
</div>
<?php require_once 'footer.php'; ?>
和my_connect.php
<?php
// display errors, comment this 2 functions after make this script to work
error_reporting(1);
ini_set('error_reporting', E_ALL);
// starting session
session_start();
// connection to database
define('DB_HOST', 'localhost'); // database host
define('DB_USER', 'root'); // database username
define('DB_PASSWORD', ''); // database pasword
define('DB_NAME', 'baza'); // database name
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
答案 3 :(得分:0)
您必须考虑它是否显示错误,这并不意味着更新已完成。这意味着查询很好(没有语法错误)。 因此,我们可以想象您正在尝试更新不存在的用户的记录。 例如:user_id = 10并且您尝试使用user_id = 11更新记录。语法没问题,SQL将找不到user_id = 11的用户,但会回答“好的,我试过,语法就可以了,所以对我来说,这很好”。
你必须考虑的是检查更新记录的数量(使用例如$ result = @mysqli_affected_rows($ handle);)但是,你必须明白,即使查询是好的,如果更新没有改变值(在你的情况下,如果“支付”的旧值和新值相同),mysqli_affected_rows将返回0。
我建议只是在通话前添加查询的“回音”。然后将此字符串复制到PhpMyadmin以查看正在发生的事情。也许在显示查询字符串时,您会看到一些值是错误的。
希望这会有所帮助。
答案 4 :(得分:0)
谢谢大家的帮助。我想通知你,我设法知道我的错误在哪里。这只是因为付款是在重复用户ID的篮子表中,这就是为什么它没有设法工作。我将它移动到用户表,它现在运行良好。