PHP:帮助修改 MySQL 数据库上的表
按照下面的示例...
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
$conexion-> insert(05,"Ford",50000000);
...我做了一个修改/更新代码:
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
$conexion-> modify("Mitsubishi", 40000000);
但它不起作用,这是错误输出:
警告:MyDataBase::modify() 缺少参数 3,调用于 第 15 行警告:MyDataBase::modify() 缺少参数 4,调用 在第 28 行中并在第 15 行中定义警告:缺少参数 5 对于 MyDataBase::modify(),在第 28 行调用并在线定义 15 AND 未定义变量 newprice、newbrand、价格。插入代码 没问题,修改代码导致了问题。
这是完整的代码:
<?php
class MyDataBase{
private $link;
public function __construct($server,$user,$password,$base){
//Conectar
$this->link = mysql_connect($server,$user,$password);
mysql_select_db($base,$this->link);
}
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
public function __destruct(){
//desconectar
}
}
$conexion = new MyDataBase ('localhost', 'root', '','crcars');
$conexion-> insert(05,"Ford",50000000);
$conexion-> modify("Mitsubishi", 40000000);
?>
Following this following example...
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
$conexion-> insert(05,"Ford",50000000);
...I made a modify/update code:
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
$conexion-> modify("Mitsubishi", 40000000);
But it didn't work, this is the error output:
Warning: Missing argument 3 for MyDataBase::modify(), called in on
line 15 Warning: Missing argument 4 for MyDataBase::modify(), called
in on line 28 and defined in on line 15 Warning: Missing argument 5
for MyDataBase::modify(), called in on line 28 and defined in on line
15 AND undefined variable newprice, newbrand, price. THE Insert code
is fine, and the modify code is causing the issue.
Here's the full code:
<?php
class MyDataBase{
private $link;
public function __construct($server,$user,$password,$base){
//Conectar
$this->link = mysql_connect($server,$user,$password);
mysql_select_db($base,$this->link);
}
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
public function __destruct(){
//desconectar
}
}
$conexion = new MyDataBase ('localhost', 'root', '','crcars');
$conexion-> insert(05,"Ford",50000000);
$conexion-> modify("Mitsubishi", 40000000);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新查询中存在无效语法。您不需要 VALUES 部分。应该是这样的:
There is an invalid syntax in the update query. You do no need the VALUES part. Here is how it should be:
您的 UPDATE 语法无效,就像@Martin 指出的那样,您还使用两个参数调用modify(),但该函数需要 5。将额外的值添加到
$conexion->;修改(“三菱”,40000000);
类似:
Your UPDATE syntax is invalid, like @Martin pointed out, also you're calling modify() with two parameters, but the function is expecting 5. Add the extra values to
$conexion-> modify("Mitsubishi", 40000000);
Something like: