<html>
<head>
<title>Inserts</title>
</head>
<body>
<h1>Adding a User</h1>
<form action="insert1.php" method="post">
Gamertag:<br />
<input name="insertfield1" type="text">
<br />
Bio:<br />
<input name="insertfield2" type="text">
<br />
Membership:<br />
<input name="insertfield3" type="text">
<br />
Gamerscore:<br />
<input name="insertfield4" type="number">
<br />
Email:<br />
<input name="insertfield5" type="text">
<br />
Name:<br />
<input name="insertfield6" type="text">
<br />
Location:<br />
<input name="insertfield7" type="text">
<br />
Friend:<br />
<input name="insertfield8" type="text">
<br />
<input type="submit" value="Insert">
</form>
</body>
</html>
结束HTML 开始php
</head>
<body>
<h1>Insert Test Results</h1>
<?php
// create short variable names
$insertfield1=$_POST["insertfield1"];
$insertfield2=$_POST["insertfield2"];
$insertfield3=$_POST["insertfield3"];
$insertfield4=$_POST["insertfield4"];
$insertfield5=$_POST["insertfield5"];
$insertfield6=$_POST["insertfield6"];
$insertfield7=$_POST["insertfield7"];
$insertfield8=$_POST["insertfield8"];
//use trim function to strip whitespace inadvertently entered at beginning or end of insert term
$insertfield1= trim($insertfield1);
$insertfield2= trim($insertfield2);
$insertfield3= trim($insertfield3);
$insertfield4= trim($insertfield4);
$insertfield5= trim($insertfield5);
$insertfield6= trim($insertfield6);
$insertfield7= trim($insertfield7);
$insertfield8= trim($insertfield8);
echo "First insert term entered: $insertfield1";
echo "<br>";
echo "Second insert term entered: $insertfield2";
echo "<br>";
echo "Third insert term entered: $insertfield3";
echo "<br>";
echo "Fourth insert term entered: $insertfield4";
echo "<br>";
echo "Fifth insert term entered: $insertfield5";
echo "<br>";
echo "Sixth insert term entered: $insertfield6";
echo "<br>";
echo "Seventh insert term entered: $insertfield7";
echo "<br>";
echo "Eight insert term entered: $insertfield8";
echo "<br>";
if (!$insertfield1)
{
echo 'You have not entered insert details. Please go back and try again.';
exit;
}
// connect to database
$link=mysqli_connect("yourMysqlServer", "usernameHere", "myPassword", "myDatabase")
or die('Could not connect ');
echo "Connected successfully <br>";
// insert new data into table
$result = mysqli_query($link,"INSERT INTO userprofile values ('$insertfield1', '$insertfield2', '$insertfield3', $insertfield4, '$insertfield5', '$insertfield6', '$insertfield7', '$insertfield8' ) " )
or die("Query failed ");
echo "query ok\n";
$num_results = mysqli_num_rows($result);
echo '<p>Number of rows inserted in userprofile: '.$num_results.'</p>';
// query table to show new data inserted
$result = mysqli_query($link,"SELECT * from userprofile")
or die("Query failed ");
echo "query ok\n";
$num_results = mysqli_num_rows($result);
echo '<p>New number of rows in userprofile: '.$num_results.'</p>';
// show results of table with new data inserted
echo " <table border='1'>\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
//Free result set
mysqli_free_result($result);
//close connection
mysqli_close($link);
?>
</body>
</html>
由于某些原因,这些没有正确地相互通信。我的假设是我的PHP代码有问题。我可以告诉你我可以正确连接,但当我尝试插入时没有,它说查询失败。
答案 0 :(得分:1)
1)尝试使用此格式
mysqli_query($link,"INSERT INTO userprofile (col1,col2,col3) VALUES ('value1','value2','value3')");
并确保你不会错过任何事情。
2)尝试将gamescore的值更改为varchar,并在插入时进行更改。
3)检查每个表列中的允许值长度,确保输入未超过允许的长度