我有2页..一个登录页面(login.php)和一个用户页面(useracc-test.php)..每次登录useracc-test-php时,我都可以从我的表中检索数据(用户) )但我无法将新数据插入另一个名为(useradvert)的表中。我有2个表,但是我试图插入的数据是一个名为(useradvert)的表。我收到这个错误。非常感谢您的帮助.. tq。
下面是我的用户页面的脚本(useracc-test.php):
<?php
//useracc-test.php
/**
* Start the session.
*/
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
// require 'lib/password.php';
require 'connect-test.php';
$userName= isset($_POST['username']) ? $_POST['username'] : '';
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
?>
<html>
<head>
<style type="text/css">
#apDiv2 {
position: absolute;
left: 51px;
top: 238px;
width: 237px;
height: 93px;
z-index: 1;
}
#apDiv1 {
position: absolute;
left: 134px;
top: 123px;
width: 234px;
height: 104px;
z-index: 2;
}
#apDiv3 {
position: absolute;
left: 58px;
top: 146px;
width: 219px;
height: 61px;
z-index: 2;
}
#apDiv4 {
position: absolute;
left: 302px;
top: 102px;
width: 365px;
height: 123px;
z-index: 3;
}
</style>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css">
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
</head>
<body>
Your Personal details:</p>
<p><?php while($row = $res->fetch_array()): ?>
<p><?php echo $row['id']; ?></p>
<p><?php echo $row['name']; ?></p>
<p><?php echo $row['username']; ?></p>
<p><?php echo $row['telno']; ?>
<?php
// $userid = $_POST['id'];
$stmt=$conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2) VALUES (?,?,?,?)");
$stmt->bind_param("isss", $id, $name2, $color2, $hobby2);
$stmt->execute();
if (!$stmt)
{ printf("Errormessage: %s\n", $mysqli->error);}
else {
echo "New records created successfully";}
$stmt->close();
$conn->close();
?>
<form name="form2" method="post" action="useracc-test.php">
<p>INSERT YOUR INTEREST:</p>
<p>
</p>
ID:
<input name="id" type="hidden" id="id" value="<?php echo $row['id']; ?>">
<p>Name :
<input type="text" name="name2" id="name2">
</p>
<p>
<label for="warna2"></label>
Color :
<input type="text" name="color2" id="color2">
</p>
<p>
<label for="hobi2"></label>
Hobby:
<input type="text" name="hobby2" id="hobby2">
</p>
<p>
<input type="submit" name="submit" id="submit" value="submit">
</p>
<p> </p>
</form>
<?php endwhile; ?>
</body>
</html>
--
-- Table structure for table `useradvert`
--
CREATE TABLE IF NOT EXISTS `useradvert` (
`id` int(10) unsigned NOT NULL,
`name2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`color2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`hobby2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`telno` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=96 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`, `telno`, `username`, `password`, `date`) VALUES
(95, 'Test Name', '09999999999', 'test@test.com', '$2y$12$fqdmAQk5c8qk8Eh2TWy2n.AdNO.lFjqmi2ruSzk8tsVXcK71OcPae', '2015-12-24 05:00:13');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `useradvert`
--
ALTER TABLE `useradvert`
ADD CONSTRAINT `useradvert_ibfk_1` FOREIGN KEY (`id`) REFERENCES `users` (`id`);
答案 0 :(得分:4)
你的问题就在这里:
$sql = sprintf ("INSERT INTO useradvert (ID, name2, color2, hobby2) VALUES (%d, '%s', '%s', '%s')", (int)
// V- here
$result['ID'], // <- here
// ^- here
mysqli_real_escape_string($conn, $name2),
mysqli_real_escape_string($conn, $color2),
mysqli_real_escape_string($conn, $hobby2));
正如错误消息所示,您无法作为数组访问mysqli_result
- 您必须先将每行fetch()
放入一个数组中。此外,即使它是有效的语法,我也无法看到ID
的值来自哪里,因为它不是查询产生结果的列之一。
你真的应该使用预备语句。除了防止SQL注入攻击之外,预准备语句还处理查询参数的所有引用和类型转换。您的第一个查询(选择)将写为
$query = "SELECT name, username, telno
FROM users
WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
你的第二个看起来像
$userid = $_POST['ID'];
$query = "INSERT INTO useradvert
(id, name2, color2, hobby2)
VALUES
(?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_params('isss', $userid, $name2, $color2, $hobby2);
$stmt->execute();
(请注意,在这两种情况下,我都省略了任何类型的错误处理,我更愿意将其作为练习留给读者。)
对于主键,您可以使用 唯一 标识该行的任何列(或列集),并且 在此期间不会更改行的生命周期 。
答案 1 :(得分:0)
我解决了两个问题......感谢达尔文的指导......但我仍然无法将数据插入到useradvert表中。我的代码出了什么问题?
$query = "SELECT name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
<p><?php while($row = $res->fetch_array()): ?>
<p><?php echo $row['name']; ?></p>
<p><?php echo $row['username']; ?></p>
<p><?php echo $row['telno']; ?>
<?php endwhile; ?>
************************
<?php
$stmt=$conn->prepare("INSERT INTO useradvert (name2,color2,hobby2) VALUES (?,?,?)");
$stmt->bind_param("sss", $name2, $color2, $hobby2);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();?>
答案 2 :(得分:0)
难怪数据没有插入..忘了if(isset($ _ POST [&#39; submit&#39;])).. LoL