我知道在stackoverflow上有很多像這樣的問題,但沒有人銷售我的問題。向數據庫添加數據PHP
我正在處理一個事件日曆,我目前正在構建一個添加事件頁面。當頁面提交時,沒有顯示錯誤,但表單信息沒有上傳到數據庫,我不確定爲什麼我檢查了語法並且看不到任何錯誤。
* addes loginform.php和core.php請求顯示$ _SESSION ['user_id']。來自 代碼: loginform.php
<?php
$con = mysql_connect("localhost","pytsuemg_brodie","brodie");
$db = mysql_select_db("pytsuemg_brodie");
if (isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash =md5($password);
if (!empty($username)&&!empty($password)) {
$query="SELECT `AccountID` FROM `Parent` WHERE `Username`='$username' AND `Password`='$password_hash'";
if ($query_run = mysql_query($query)) {
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0) {
echo 'Invalid username/password combination';
} else
if ($query_num_rows==1) {
$user_id = mysql_result($query_run,0,'AccountID');
$_SESSION['user_id']=$user_id;
header('Location: profilepage.php');
}
}
} else {
echo 'You must supply a username and password.';
}
}
?>
<form action="login.php" method="POST">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Log In">
</form>
core.php中
<?php
ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];
function loggedin() {
if (isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])) {
return true;
} else {
return false;
}
}
?>
addevent.php
require 'core.php';
$con=mysqli_connect("localhost","pytsuemg_brodie","brodie","pytsuemg_brodie");
if (isset($_POST['DateStart'])&&isset($_POST['DateEnd'])&&isset($_POST['TimeStart'])&&isset($_POST['TimeEnd'])&&isset($_POST['EventType'])&&isset($_POST['Description'])&&isset($_POST['Children'])&&isset($_POST['Location'])&&isset($_POST['MealsGiven'])){
$DateStart = $_POST['DateStart'];
$DateEnd = $_POST['DateEnd'];
$TimeStart = $_POST['TimeStart'];
$TimeEnd = $_POST['TimeEnd'];
$EventType = $_POST['EventType'];
$Description = $_POST['Description'];
$Children = $_POST['Children'];
$Location = $_POST['Location'];
$MealsGiven = $_POST['MealsGiven'];
if (!empty($DateStart)&&!empty($DateEnd)&&!empty($TimeStart)&&!empty($TimeEnd)&&!empty($EventType)&&!empty($Description)&&!empty($Children)&&!empty($Location)&&!empty($MealsGiven)){
$sql = "INSERT INTO Events (DateStart, DateEnd, TimeStart, TimeEnd, Location, EventType, MealsGiven, description,Children, AccountID) VALUES(
'$DateStart','$DateEnd','$TimeStart','$TimeEnd','$Location','$EventType','$MealsGiven','$Description','$Children','".$_SESSION['user_id']."')";
mysqli_query($con, $sql);
/* commit transaction */
if (!mysqli_commit($con)) {
print("Transaction commit failed\n");
exit();
}
} else {
echo'All fields are required';
}
}
/* close connection */
mysqli_close($con);
?>
<br>
<form action="addevent.php" method="post">
<p> Date:<label for="from"> From</label>
<input type="text" id="from" name="DateStart">
<label for="to">to</label>
<input type="text" id="to" name="DateEnd"></p>
<p>Start Time:<input type="text" name="TimeStart" /></p>
<p>End Time:<input type="text" name="TimeEnd" /></p>
<p>Event Type :
<select name="EventType">
<option value="Change over">Changeover</option>
<option value="Parents Evening">Parents Evening</option>
<option value="After School Activity">After School Activity</option>
<option value="Weekend Activity">Weekend Activity</option>
<option value="Holiday">Holiday</option>
</select> </p>
<p>Description (max 100 Characters) <br><textarea rows="2" cols="50" maxlength="100" name="Description"></textarea></p>
<p>Children Involved: <br><textarea rows="1" cols="50" maxlength="32" name="children"></textarea></p>
<p>Location: <input type="text" name="Location" /></p>
<p>Meals Given: <select name="MealsGiven">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select></p>
<p><input type="submit" value="Submit"/></p>
</form>
我沒有看到對'$ _SESSION ['user_id']'的引用,也沒有在您的代碼中顯示'session_start();'。如果引用爲空,則整個查詢將失敗。請參考它或刪除它。 –
它在core.php中。我在其他頁面中使用$ _SESSION ['user_id'],它工作正常 – user3393477
'core.php'究竟是什麼? –