Php 使用图像更新语句
我有一个php页面,里面有用于更新记录和图像的表单,我不知道update语句有什么问题,,,字段的值被获取,我可以通过GET方法在url上看到它们。。。但是,当我运行页面时,更新记录信息没有更改,页面上也没有显示任何内容,,,由于没有字段r进行更新,我认为我的update语句有问题,,,以下是代码:Php 使用图像更新语句,php,mysql,Php,Mysql,我有一个php页面,里面有用于更新记录和图像的表单,我不知道update语句有什么问题,,,字段的值被获取,我可以通过GET方法在url上看到它们。。。但是,当我运行页面时,更新记录信息没有更改,页面上也没有显示任何内容,,,由于没有字段r进行更新,我认为我的update语句有问题,,,以下是代码: <?php // Connect to the database require("includes/conn.php"); // Script Variables
<?php
// Connect to the database
require("includes/conn.php");
// Script Variables
$target_dir = 'images/';
$file_given = false;
$inputs_given = false;
$id_given = false;
if(isset($_POST['serialid']) && $_POST['serialid'] != "")
{
$serialid = $_POST['serialid'];
$id_given = true;
}
// You only need to catch input from a create or modify action, so start by checking for ALL the REQUIRED inputs
if(isset($_POST['name']) && $_POST['name'] != "" && isset($_POST['description']) && $_POST['description'] != "" && isset($_POST['price']) && $_POST['price'] != "")
{
$name = $_POST['name'];
$paragraph = $_POST['description'];
$price = $_POST['price'];
if(isset($_POST['picture']) && $_POST['picture'] != "")
{
$picture = basename($_FILES['picture']['name']);
$file_given = true;
}
// Just some verification (not really much, but you can write your own functions and slot them in
$name_safe = true;
$description_safe = true;
$price_safe = true;
$picture_safe = false;
if($_FILES["picture"]["type"] == "image/gif" || $_FILES["picture"]["type"] == "image/jpg" || $_FILES["picture"]["type"] == "image/png" || $_FILES["picture"]["type"] == "image/bmp")
$picture_safe = true;
if($name_safe && $description_safe && $price_safe && $picture_safe)
$inputs_given = true;
}
if($id_given && $inputs_given)
{
// Search for the record and see if it exists
$get_record = mysql_query("SELECT serial, picture FROM products WHERE serial='$serialid'");
$record_exists = mysql_num_rows($get_record);
if($record_exists == 1)
{
if($file_given)
{
$update_image = ", picture='$picture'";
// Now we need to remove the old image from the file system and upload our new one in it's place
$previous_image = mysql_result($get_record,'0','picture');
unlink($target_dir . $previous_image);
//Now that the previous image has been removed, we need to upload our new image
$new_image = $target_dir . $picture ;
move_uploaded_file($_FILES['picture']['tmp_name'], $new_image);
}
else
$update_image = "";
if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))
$action_output = "Record successfully modified.";
else
$action_output = "Record modification unsuccessful.";
}
else
$action_output = "The record id you specified does not exist.";
}
?>
<html>
<head>
<title>Manage Records</title>
</head>
<body>
<?php echo $action_output; ?>
</body>
</html>
<?php
// Disconnect from the database
?>
我的修改表是这样的
<?php
// Connect to the database
require("includes/conn.php");
$id_given = false;
if(isset($_POST['serialid']) && $_POST['serialid'] != "")
{
$serialid = $_POST['serialid'];
$id_given = true;
}
if($id_given)
{
$get_record = mysql_query("SELECT * FROM products WHERE serial='$serialid'");
$record = mysql_fetch_array($get_record);
$output = '<form method="POST" enctype="multipart/form-data" action="update.php?serialid=' . $record['serialid'] . '&action=modify">
<table>
<tr>
<td>Name:</td>
<td><input name="name" type="text" value="' . $record['name'] . '"/></td>
</tr>
<tr>
<td>Description :</td>
<td><textarea name="description" cols="45" rows="5">' . $record['description'] . '</textarea></td>
</tr>
<tr>
<td>Price:</td>
<td><input name="price" type="text" value="' . $record['price'] . '"/></td>
</tr>
<td colspan="2"><img height="50" width="50" src="../images/' . $record['picture'] . '"/><br/>' . $record['picture'] . '</td>
</tr>
<tr>
<td>Modify Image:</td>
<td><input name="picture" type="file" value="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Modify Record"/>
</td>
</tr>
</table>
</form>';
}
else
$output = 'No record id was specified.';
?>
<html>
<head>
<title>Modify Record</title>
</head>
<body>
<?php echo $output; ?>
</body>
</html>
<?php
// Disconnect from the database
?>
首先,这一行在WHERE
前面有一个额外的逗号:
if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))
if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price' " . $update_image . " WHERE serial='$serialid'"))
正确的语法是:
if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))
if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price' " . $update_image . " WHERE serial='$serialid'"))
然后,你说
我可以通过GET方法在url上看到它们
但在脚本中,您使用$\u POST
变量获取值,请改用$\u get
或将表单的方法更改为POST
如果要上载图片,必须使用post
方法,文件将在$\u FILES
变量中可用。
在您的示例中,您通过URL传递参数,因此使用get
方法,“picture”只是指向PC中图片的路径,它不会上载到服务器上。
编辑:
在表单中添加“
和”
,而不是将此参数添加到表单的操作url中,它应该可以工作您已经在$update\u image=“,picture='$picture'”中添加了逗号代码>以及在
if(mysql_查询(“更新产品集名称='$name',说明='$description',价格='$price',
“$UPDATE_image.”其中serial='$serialid'))
删除$update\u image=“picture='$picture'中的逗号代码>或删除此文件
if(mysql_查询(“更新产品集名称='$name',说明='$description',价格='$price'.$UPDATE_image.”其中serial='$serialid'))
我更正了该行,但仍然没有发生任何事情:(,,pz查看我的帖子中的url我刚刚添加了它可能有助于查看我的编辑,您正在使用$\u post
变量,但您正在处理GET
请求。我将其全部更改为$\u post,但没有任何更改work@user1084949让他们到$\u POST
但是我认为您的HTML表单使用get
方法,更改我把表格也改成了POST,什么也没有change@user1084949尝试此mysql\u查询(“更新产品集名称='$name',说明='$description',价格='$price'”。$UPDATE\u image。“其中serial='$serialid'”)或die(mysql\u error());
我也尝试过,但仍然没有更改显示的空页面,并且记录r不更新它:(只是echo“更新产品集名称='$name',说明='$description',价格='$price'。$UPDATE_image.“其中serial='$serialid'”
在查询之前,请检查您是否正确获取了所有值。然后在phpMyAdmin中手动运行查询,并尝试使用$\u POST而不是$\u GET。使用$\u GET不安全。由于这些值可以通过URL看到,因此我将其全部更改为$\u POST,并且您无法在主POST上检查我的html表单