Jump to content

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


pinangsia

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]
Link to comment
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]
Link to comment
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;

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.