Jump to content

Adding data doesn't work - help needed


toxi

Recommended Posts

I'm trying to add the following set of data to my database

 

$query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES 
('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')";

 

these all correspond to a booking with the following types:

 

bookTitle: varchar(100)

date: date

bookStart: Time

bookEnd: Time

bookLanes: varchar(10)

 

Also, there's another field in my table, called id that's automatically incremented (the first column in my table)

 

After asking around I was told to use addslash but I don't see where that is needed.

Before adding this values to my database I've added the following code to verify that some input is indeed entered:

 

if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes'])));	

 

any help is appreciated

 

Link to comment
Share on other sites

here it is

<?php
//Database connection details
$host = "**";
$mysql_user = "**";
$mysql_password = "**";
$mysql_db = "**";

//make connection with mysql and select the database
$mysql_connect = mysql_connect($host, $mysql_user, $mysql_password);
$db_select = mysql_select_db($mysql_db);

//will be used to show alert message for success or error
$alert = "";

//check if the form is submitted
if(isset($_POST['add']))
{
//check for empty inputs
if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes'])));																																																																	
{
	//add new event to the database
	$query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES 
('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')";

	$result = mysql_query($query);

	//check if the insertion is ok
	if($result)
		$alert = "New Event successfully added";
	else 
		$alert = "Something is wrong. Try Again.";
}
else 
{
	//alert message for empty input
	$alert = "No empty input please";
}
}
?>

Link to comment
Share on other sites

Hum...

 

Here's the rest with the HTML code...does it make any sense ?

 

<html>
<head>
<title>Add New Events</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<link rel="stylesheet" href="datepick/jquery.datepick.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" src="datepick/jquery.datepick.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){

//configure the date format to match mysql date
$('#date').datepick({dateFormat: 'yy-mm-dd'});
});
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table align="center">
	<tr>
		<td colspan="2">
			<h2>Add a New Booking</h2>
		</td>
	</tr>
        <tr>
		<td>Booking Title : </td>
		<td><input id="bookTitle" name="bookTitle" size="50"></td>
	</tr>
	<tr>
		<td>Date : </td>
		<td><input id="date" name="date" size="30"></td>
	</tr>

	<tr>
		<td>Start Time : </td>
		<td><input id="bookStart" name="bookStart" size="10"></td>
	</tr>
        <tr>
		<td>End Time : </td>
		<td><input id="bookEnd" name="bookEnd" size="10"></td>
	</tr>        <tr>
		<td>Booked Lanes : </td>
		<td><input id="bookLanes" name="bookLanes" size="5"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="Add Event" name="add"></td>
	</tr>
</table>
</form>
<?php
//check if there is any alert message set
if(isset($alert) && !empty($alert))
{
//message alert
echo '<script type="text/javascript">alert("'.$alert.'");</script>';
}
?>
</body>
</html>

Link to comment
Share on other sites

You need to double check you parenthesis on your if statement, they don't line up (error_reporting would have found this ;))

 

Hopefully color coding the parenthesis will help you find your errors (and yes, the ones that don't line up are bolded and underlined):

 

if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes'])))

 

Also, on your query:

$query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES 
('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')";

Make sure to use curly brackets { } or concatenate the variables with a period:

$query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES 
('".$_POST['booktitle']."','".$_POST['date']."','".$_POST['bookStart']."','".$_POST['bookEnd']."','".$_POST['bookLanes']."')";

Link to comment
Share on other sites

I've scraped the if statement and the file loaded fine, but it gave me an error of no input the moment it loaded!

 

So there's a prblem with my if statement I'll be looking at. Also, the query doesn't seem to work as it'll refuse to add data on to my database.

 

and I even tried this code:

 

	$query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES 
('". addslashes($_POST['booktitle'])."','".$_POST['date']."','". addslashes($_POST['bookStart']) ."','". addslashes($_POST['bookEnd']) ."','". addslashes($_POST['bookLanes']) ."')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.