尝试将字段插入数据库表但出现错误“找不到对象”
Posted
技术标签:
【中文标题】尝试将字段插入数据库表但出现错误“找不到对象”【英文标题】:Trying to insert field in to Database table but getting error "Object Not Found" 【发布时间】:2018-10-03 10:00:32 【问题描述】:我试图在我的数据库中添加一个字段,但每次我单击插入时都会收到错误“找不到对象” 这是我的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 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 时,我可以看到带有记录的表格,但其他比我迷路了,任何帮助将不胜感激? 抱歉发了这么长的帖子
【问题讨论】:
你的表单操作太误导人了student_insert_record.php
在哪里?
你应该给表单 action="enter_details.php"
@amitMerchant 实际上应该是 game_input_form 但是当我插入它似乎仍然插入的记录时,我没有收到错误但它仍然有效?
你应该改为
【参考方案1】:
在你的 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 插入值。
希望这能解决它。 :)
【讨论】:
以上是关于尝试将字段插入数据库表但出现错误“找不到对象”的主要内容,如果未能解决你的问题,请参考以下文章