Jump to content

Archived

This topic is now archived and is closed to further replies.

pinangsia

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

Recommended Posts

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==>
[code]<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>[/code]

script 2==>
[code]<?
//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>[/code]

Share this post


Link to post
Share on other sites
Can you post the file/script that has the form used to enter data, please?

Share this post


Link to post
Share on other sites
Thanks in advance andyB,

[code]<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>[/code]

Share this post


Link to post
Share on other sites
You're not specify the HTML name attribute correctly for your INPUT fields... missing equals sign.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
I don't think it's wrong... it's syntactically correct.

Share this post


Link to post
Share on other sites

×

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.