I'm quite new to php although i understand the foundation of php. Most of it atleast.
there are 2 problems with my php script.
One: Even though i typed-in the information inside the text box it always directs me to the ?insert=empty im not sure why it's always telling me that one of the boxes are empty even though it isn't. is it the input type="date"?.
Two: I tried removing the if(empty) method leaving the mysqli query. And it works just fine although no data was inserted into the database.
PHP
session_start();
if (isset($_POST['submit'])) {
include 'includes/dbh.inc.php';
$id = $_GET['id'];
$c_id = $_REQUEST['u_uid'];
$e_name = mysqli_real_escape_string($conn, $_POST['e_name']) ;
$d_event = mysqli_real_escape_string($conn, $_POST['d_event']) ;
$t_event = mysqli_real_escape_string($conn, $_POST['t_event']) ;
$e_t_event = mysqli_real_escape_string($conn, $_POST['e_t_event']) ;
$theme = mysqli_real_escape_string($conn, $_POST['theme']) ;
$date = date('Y-m-d H:i:s');
if (empty($e_name) || empty($d_event) || empty($t_event) || empty($e_t_event) || empty($theme)){
header("Location: ../package.all.php?insert=empty");
exit();
}else{
$sql = "INSERT INTO event_table (event_name, event_date, event_time_start, event_time_end, cusact_id, theme, reserve_date_time, package_id) VALUES ('$e_name', '$d_event', '$t_event', '$e_t_event', '$theme', '$date', '$id');";
mysqli_query($conn, $sql);
header("Location: ../package.all.php?insert=success");
}
}
else {
echo 'isset was false';
header("Location: ../inquire-packages.inc.php?insert=failed");
}
HTML
<form action="includes/inquire-packages.inc.php" method="POST">
<label>Event Name</label>
<input type="text" name="e_name" placeholder="Enter Event Name*"/>
<label for="job">Day of the Event</label>
<input type="date" name="d_event" placeholder="Enter Date of Event*"/>
<label for="job">Time of the Event</label>
<input type="Time" name="t_event" placeholder="Enter Time of Event*"/>
<label for="job">End Time of the Event</label>
<input type="Time" name="e_t_event" placeholder="Enter End Time of Event*"/>
<label>Theme</label>
<input type="text" name="theme" placeholder="Enter Theme*"/>
<input name="submit" type="submit" value="Submit"/>
</form>
I'm sorry if this is a stupid question but i really want to learn php code.
Thank you, Omar
1st : Column count
doesn't match with value
count
. your column count is 8
but value count 7
. so you missed one value . try to debug it .
2nd : I guess you missed $c_id
.
3rd : Try to use prepared statement to avoid sql injection .
4th : Header()
function doesn't terminate the code execution . so use exit()
after header function .
5th : Add error handler mysqli_query($conn, $sql) or die(mysqli_error($conn));
6th : And where from you get these two values $id = $_GET['id'];$c_id = $_REQUEST['u_uid'];
PHP :
$sql = "INSERT INTO event_table (event_name,
event_date,
event_time_start,
event_time_end,
cusact_id,
theme,
reserve_date_time,
package_id)
VALUES ('$e_name',
'$d_event',
'$t_event',
'$e_t_event',
'$c_id',
'$theme',
'$date',
'$id');";
mysqli_query($conn, $sql) or die(mysqli_error($conn));
Checking the documentation of mysqli_real_escape_string
, I found that if the connection is not open, the output will be an empty string, so try to check if the connection is open:
if (mysqli_connect_errno()) {
printf("%s
", mysqli_connect_error());
exit();
}