我可以将数据插入上一个受影响的行吗 首先,我使用data.php在'198'行插入数据 第二个使用image.php更新行'198'本身的url_path,但是添加为下一行。
PHP代码 -
$file = $_FILES['image']['tmp_name'];
$con=mysqli_connect("host","user","password","db");
// Check connection
if (mysqli_connect_errno()) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
try{
if (!isset($_FILES['image']['tmp_name'])) {
echo "Image not recognised";
}else{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
//folder name 'images'
move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
$location="http://localhost/folder/images/" . $_FILES["image"]["name"];
}
$result = mysqli_query($con, "INSERT INTO products (pro_img_path) VALUES ('$location')");
$response["success"] = 1;
$response["message"] = "Image Added Successful!";
die(json_encode($response));
}
catch(Exception $e){
$response["success"] = 0;
$response["message"] = "Image Not Added";
die(json_encode($response));
}
mysqli_close($con);
?>
任何帮助
已编辑的代码
try{
if (!isset($_FILES['image']['tmp_name'])) {
echo "Image not recognised";
}else{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
//folder name 'images'
move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
$location="http://localhost/folder/images/" . $_FILES["image"]["name"];
}
$result = mysqli_query($con, "INSERT INTO products (pro_img_path) VALUES ('$location')");
$lastid=mysqli_insert_id($con);
$result = mysqli_query($con, "UPDATE products SET pro_img_path='$location' WHERE time='$lastid' LIMIT 1");
$response["success"] = 1;
$response["message"] = "Image Added Successful!";
die(json_encode($response));
}
**Table Structure**
-- phpMyAdmin SQL Dump
-- version 4.1.6
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Sep 15, 2014 at 08:40 PM
-- Server version: 5.6.16
-- PHP Version: 5.5.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `db`
--
-- --------------------------------------------------------
--
-- Table structure for table `products`
--
CREATE TABLE IF NOT EXISTS `products` (
`pid` int(100) NOT NULL AUTO_INCREMENT,
`category` varchar(20) COLLATE utf8_bin NOT NULL,
`subcategory` varchar(20) COLLATE utf8_bin NOT NULL,
`product_name` varchar(100) COLLATE utf8_bin NOT NULL,
`product_descrip` varchar(255) COLLATE utf8_bin NOT NULL,
`product_price` varchar(100) COLLATE utf8_bin NOT NULL,
`more_info` varchar(255) COLLATE utf8_bin NOT NULL,
`pro_img_path` varchar(255) COLLATE utf8_bin NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Product Details' AUTO_INCREMENT=185 ;
--
-- Dumping data for table `products`
--
INSERT INTO `products` (`pid`, `category`, `subcategory`, `product_name`, `product_descrip`, `product_price`, `more_info`, `pro_img_path`, `time`) VALUES
(176, 'Electronics', 'Mobile Accessories', 'Samsung Charger', 'Model G-S5830', '0890', 'MegaWat 10', '', '2014-09-15 14:19:11'),
(182, 'Category1', 'Subcategory1', 'Electronics', 'Camera', 'Sony', 'ZeroShot', '', '2014-09- 15 17:35:42');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
答案 0 :(得分:0)
如果要在插入数据后更新数据,请执行以下操作:
代码
之后$result = mysqli_query($con, "INSERT INTO products (pro_img_path) VALUES ('$location')");
找出插入ID并将其分配给变量http://cz2.php.net/manual/en/mysqli.insert-id.php
$lastid=mysqli_insert_id($con);
然后运行类似于此的更新查询
UPDATE products SET column=value WHERE id='$lastid' LIMIT 1
请注意limit 1子句,如果您是php和mysql的新手,可以防止大量错误。如果您计划更新一行,则将查询限制为一行,以防您搞乱WHERE子句。
以下是完整的更新语法: http://dev.mysql.com/doc/refman/5.0/en/update.html 这是mysqli文档: http://php.net/manual/en/book.mysqli.php
答案 1 :(得分:0)
你可以使用:mysqli_insert_id来获取最后插入的id
$last_inserted_id=mysqli_insert_id($con) ;
然后只使用您的值SET
进行更新UPDATE products SET column1=value1,column2=value2 WHERE id='$last_inserted_id';