Jump to content


Photo

please let me know what i did wrong? been trying 2 days now:(


  • Please log in to reply
7 replies to this topic

#1 pinangsia

pinangsia
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 05 September 2006 - 12:01 AM

Calling all experts,
as a newbie, i'm trying to insert data from a html form to mysql, please see my below php scripts. i'm using php5.0.2, mysql 4.1 and apache 2.0.52
on script1, its always telling me that i have not entered all data, if i get rid of this line
if (!$isbn || !$author || !$title || !$price),
it actually works, but nothing in mysql database, just empty row.

same on script2 only empty rows.

please let me know what i did wrong.
all feedback is very very much appreciated.

script1==>
<html>
<head><title> Book entry homepage</title>
</head>
<body>
<h1> Book Entry Results</h1>

<?php
//create short var names
$isbn = $HTTP_POST_VARS['isbn'];
$author = $HTTP_POST_VARS['author'];
$title = $HTTP_POST_VARS['title'];
$price = $HTTP_POST_VARS['price'];

if (!$isbn || !$author || !$title || !$price)
{
	echo 'You have not entered all required details <br />'.'Please go back and try again.';
	exit;
}

$isbn = addslashes($isbn);
$author = addslashes($author);
$title = addslashes($title);
$price = doubleval($price);

@ $db = mysql_pconnect ('localhost', 'root', 'password');

if (!$db)
{
	echo 'cannot connect to database try again later';
	exit;
}
mysql_select_db ('testaj');
$query = "INSERT INTO books VALUES 
	('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result = mysql_query($query);

if ($result)
	echo mysql_affected_rows().'book entered into database.';
?>
</body>
</html>

script 2==>
<?
//create short var names
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

/*if ($firstName or $lastName )
{
	echo 'You have not entered all required details <br />'.'Please go back and try again.';
	exit;
}

$firstName = addslashes($firstName);
$lastName = addslashes($lastName);
*/
@ $db = mysql_pconnect ('localhost', 'root', 'password');

if (!$db)
{
	echo 'cannot connect to database try again later';
	exit;
}
mysql_select_db ('demo');
$query = "INSERT INTO members (firstName, lastName) VALUES 
	('".$firstName."', '".$lastName."')";
$result = mysql_query ($query);

if ($result)
	echo mysql_affected_rows() .'name entered into database.';
?>
</body>
</html>


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 05 September 2006 - 01:07 AM

Can you post the file/script that has the form used to enter data, please?
Legend has it that reading the manual never killed anyone.
My site

#3 pinangsia

pinangsia
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 05 September 2006 - 02:15 AM

Thanks in advance andyB,

<html>
<head> <title> New Book Entry </title>
</head>
<body>
<h1> Book Entry </h1>

<form action= "insert_book.php" method= "post">
	<table border="0">
		<tr>
			<td>ISBN</td>
			<td><input type="text" name "isbn" maxlength="13" size="13"><br /></td>
		</tr>

		<tr>
			<td>Author</td>
			<td><input type="text" name "author" maxlength="30" size="30"><br /></td>
		</tr>

		<tr>
			<td>Title</td>
			<td><input type="text" name "title" maxlength="60" size="30"><br /></td>
		</tr>

		<tr>
			<td>Price %body%lt;/td>
			<td><input type="text" name "price" maxlength="7" size="7"><br /></td>
		</tr>

		<tr>
			<td colspan="2"><input type="submit" value ="Register book now"></td>
		</tr>

	</table>
</form>

</body>
</html>


and form for script 2==>

<html>
<head> <title> New Name Entry </title>
</head>
<body>
<h1> NAME ENTRY </h1>

<form action= "insert_name.php" method= "post">
	<table border="0">
		<tr>
			<td>First Name</td>
			<td><input type="text" name 'firstName' maxlength="50" size="10"><br /></td>
		</tr>

		<tr>
			<td>lastName</td>
			<td><input type="text" name 'lastName' maxlength="50" size="10"><br /></td>
		</tr>

		<tr>
			<td colspan="2"><input type="submit" value ="put names into database"></td>
		</tr>

	</table>
</form>

</body>
</html>


#4 pinangsia

pinangsia
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 05 September 2006 - 11:44 AM

Anyone?
its nearly 3 days now,pls don't let me throw in my towel :((

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 September 2006 - 01:02 PM

You're not specify the HTML name attribute correctly for your INPUT fields... missing equals sign.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 pinangsia

pinangsia
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 05 September 2006 - 09:18 PM

you meant like this one
<td><input type="text" name "isbn" maxlength="13" size="13"><br /></td>
must be
<td><input type="text" name="isbn" maxlength="13" size="13"><br /></td>

i couldn't wait to try it...  Thanks Fenway

Anyone can explain why this one doesn't work? it keeps on saying that i have not entered all values. 

if (!$isbn || !$author || !$title || !$price)
{
echo 'You have not entered all required details <br />'.'Please go back and try again.';
exit;



#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 September 2006 - 10:10 PM

I don't think it's wrong... it's syntactically correct.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#8 pinangsia

pinangsia
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 06 September 2006 - 09:28 PM

Its all good now.
Thank you Fenway.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users