嘿傢伙我一直在拉我的頭髮,試圖找出爲什麼這個php代碼不工作。基本上我有數據庫和表都設置正確,我試圖讓用戶填寫的數據去到MySQL數據庫。數據沒有被插入MySQL數據庫使用PHP
我一直在Google上搜索幾個小時,還沒有找到解決方案。當我單擊頁面上的提交時,頁面刷新並看起來像數據已提交,但查詢數據庫時不顯示任何內容。
<form action="#" method="post">
<div class="row">
<h4>Select your school</h4>
<p>If you can't find it, contact your administrator about signing your school up!</p>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
Choose your school from the dropdown menu
<span class="caret"></span>
</button>
<select title="Select your School" name="School" id="schools">
<option value="1">University of Central Florida</option>
<option value="2">Seminole State College</option>
<option value="3">School of Hard Knocks</option>
</select>
</div>
</div>
<div class="row">
<h4>Name</h4>
<input class="form-group col-lg-4" id="first" name="first" type="text" placeholder="First">
<input class="form-group col-lg-4" id="last" name="last" type="text" placeholder="Last">
</div>
<div class="row">
<h4>Email Address</h4>
<input class="form-group col-lg-8" id="email" name="email" type="text" placeholder="ex. [email protected]">
</div>
<div class="row">
<h4>Password</h4>
<input class="form-group col-lg-8" id="password" name="password" type="text" placeholder="ex. Hunter2">
</div>
<div class="row">
<input id="submit" name="submit" type="submit" value="submit" class="btn btn-primary">
</div>
</form>
</div> <!-- /container -->
<?php
error_reporting(E_ALL);
if (isset($_POST['submit']))
{
$firstName = -1;
$lastName = -1;
$email = -1;
$password = -1;
$school = -1;
$dropdown_val = -1;
$connect=mysqli_connect('localhost','root','yanni123','eventmanager');
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
$firstName = $_POST["first"];
$lastName = $_POST["last"];
$email = $_POST["email"];
$password = $_POST["password"];
$dropdown_val = $_POST["School"];
mysqli_query($connect, "INSERT INTO users (idusers, firstName, lastName, password, emailAddress, school)
VALUES (1, '$firstName', '$lastName', '$password', '$email', '$dropdown_val')");
mysqli_close($connect);
}
?>
使用'mysqli_error'添加mysqli_query'後'錯誤處理。併發布錯誤消息 – amdixon
檢查錯誤。您也可以使用此代碼開放SQL注入。 http://php.net/manual/en/mysqli.error.php檢查連接和查詢。 – chris85
好的,我會添加錯誤處理,並讓你知道,我試圖做到這一點,但我無法得到它的工作。此外,此代碼將永遠不會在線用於學習目的,但是一旦我開始工作,我肯定會添加SQL注入檢查。 – YanniGen