我尝试在数据库中添加一个字段,但每次单击插入时都会收到错误“未找到对象” 这是我的 game_input_form.php
<html><head><title>Games Insert Form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: 10pt }
</style>
</head>
<body>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="64b1ff">
<h3>Insert Game</h3>
<form method="GET" action="enter_details.php">
Enter Game ID: <input type=text name=gameid size=30><br>
Enter Name: <input type=text name=name size=30><br>
Enter Platform: <input type=text name=platform size=30><br>
Enter Price :<br> <input type=text name=price size=20><br>
<br>
<input type=submit value=Insert><input type=reset>
</form>
</td></tr></table>
</body>
</html>
我的gametest.php(我在这个文件中找不到任何错误,看起来不错)
<?php
$host="localhost";
$user="root";
$password="";
$con=mysqli_connect($host,$user,$password) or die(mysql_error());
echo "Connected to MySQL<br/>";
mysqli_select_db($con,"gamedb") or die(mysqli_error());
echo "Connected to Database";
// Create a MySQL table in the selected database
$query=mysqli_query($con,"CREATE TABLE gameinfo(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
platform VARCHAR(30),
price DECIMAL)")
or die(mysqli_error());
echo "Table Created!";
?>
这是我的 game_display_records.php
<html>
<head><title>Display Records</title>
<style type="text/css">
th, td {font-family: tahoma, arial, verdana; font-size: 10pt; font-weight: 500 }
</style>
</head>
<body>
<?php
$db="gamedb";
$link = mysqli_connect('localhost', 'root', '',$db);
if (mysqli_connect_errno()) {
trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
}
$result = mysqli_query($link, "SELECT * FROM gameinfo" ) or die("SELECT Error: ".mysqli_error($link));
$num_rows = mysqli_num_rows($result);
print "There are $num_rows records.<br><br>";
/* Display a html table */
print "<table width=600 border=1>";
print "<tr><th>ID</th><th>Name</th><th>Platform</th><th>Price</th></tr>";
/* Outer loop using mysqli fetch row function to extract a single record and store it in php variable $get_info */
while ($get_info = mysqli_fetch_row($result)){
print "<tr>";
/* Inner foreach loop to extract a single field from $get_info and store it in php variable $field */
foreach ($get_info as $field)
print "<td>$field</td>"; //display the field as a table cell
print "</tr>";
}
print "</table>";
mysqli_close($link);
?>
<br>
<form method="POST" action="mainForm.php">
<input type="submit" value="Database Interface">
</form>
</body>
</html>
这是我的enter_details.php
<html><head><title>Student Insert Record</title></head>
<body>
<?php
$gameNum=$_GET['gameid'];
$name =$_GET['name'];
$platform=$_GET['platform'];
$price=$_GET['price'];
$db="gamedb";
$db = new mysqli('localhost', 'root', '', 'gamedb');
if($db->connect_errno > 0){
die('Error : ('. $db->connect_errno .') '. $db->connect_error);
}
$result=mysqli_query($db,"INSERT INTO gameinfo (gameid, name, platform, price)
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));
mysqli_close($db); // Close the connection to the mysql server
print "Record added";
?>
<form method="POST" action="game_input_form.php">
<input type="submit" value="Insert Another game Record">
</form>
<br>
<form method="POST" action="mainForm.php">
<input type="submit" value="Back to Student Records System Menu">
</form>
</body>
</html>
我浏览了每个页面,但似乎找不到错误,我真的不知道哪里出了问题,当我在浏览器中进入 myphpadmin 时,我可以看到包含记录的表,但其他比我迷路了,任何帮助将不胜感激? 很抱歉这篇文章很长
最佳答案
在您的enter_details.php中您有插入语句。
$result=mysqli_query($db,"INSERT INTO gameinfo (gameid, name, platform, price)
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));
但不应该是
$result=mysqli_query($db,"INSERT INTO gameinfo (id, name, platform, price)
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));
根据您的 gametest.php,您正在创建一个具有列名称 id 的表,并且您正在将值插入到列名称 gameid 中。
希望这能解决这个问题。 :)
关于php - 尝试将字段插入到数据库表中,但出现错误 "Object Not Found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979691/