-7
我有插入數據的挑戰。所有東西都會檢出......即連接到數據庫。數據不能插入。我正在使用本地主機xampp。 我insert.php使用php插入數據到mysql時出錯insert.php
<?php
$servername = "localhost";
$username = "root";
$password = "Maweutah2";
$dbname = "digital_content";
$link = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$ref = mysqli_real_escape_string($link, $_REQUEST['ref']);
$date = mysqli_real_escape_string($link, $_REQUEST['date']);
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$class = mysqli_real_escape_string($link, $_REQUEST['class']);
$serial = mysqli_real_escape_string($link, $_REQUEST['serial']);
$details = mysqli_real_escape_string($link, $_REQUEST['details']);
$remarks = mysqli_real_escape_string($link, $_REQUEST['remarks']);
// attempt insert query execution
$sql = "INSERT INTO laptop_repair (ref, name, class, serial, details, remark) VALUES ('$ref', '$date', '$name', '$class', '$serial', '$details', '$remarks')";
if(mysqli_query($link, $sql)){
echo "<h2>Records added successfully<a href='laptop_repairs.php'>ENTER AGAIN</h2></a>.";
} else{
echo "<h2>ERROR: TRY AGAIN <a href='db.php'>ENTER AGAIN</h2></a>";
}
// close connection
mysqli_close($link);
?>
我的形式
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div class="center">
<h1>ENTER DETAILS</h1>
<form action="insert.php" method="POST">
<h3>REF NUMBER</h3><input type="text" name="ref" /><br>
<h3>DATE</h3><input type="text" name="date" /><br>
<h3>NAME</h3><input type="text" name="name" /><br>
<h3>CLASS</h3><input type="text" name="class" /><br>
<h3>SERIAL</h3><input type="text" name="serial" /><br>
<h3>DETAILS</h3><input type="text" class="inputmingi" name="details" /><br>
<h3>REMARKS</h3><input type="text" class="inputmingi" name="remarks" /><br>
<input type="submit" />
</form>
</div>
<div class="center">
<h1>MENU</h1>
<form action="view.php" method="POST">
<button type="submit">View records</button>
</form>
</div>
</html>
當我提交,連接成功,但在我的表中沒有數據插入。
你得到的錯誤信息是什麼? – Viktor
*你的意思是什麼?「但我的表格中沒有插入任何數據。」*? mysqli_query()成功了嗎?如果沒有,使用['mysqli_error()'](http://php.net/manual/en/mysqli.error.php)找出原因。 – axiac
我看到6個字段和7個值。 –