Jump to content

New to PHP - first problem post (of many i would asume)


RichG

Recommended Posts

Hi Everyone

 

I am new to this forum and need to look around at the content to know what information is already out there. So I am sorry of this has already been posted.

 

I am working my way through a 'php and mysql web development' book and I have a small problem.

 

The server is not reading one of my lines in my script. I know there are a few typo's in the book so can you let me know if this is all correct. I am not getting errors from the server it is just not reading my input form my form.

 

I have created a form input page which will input values into a database once submitted (see here http://www.richdgrimes.co.uk/phptest/newbook.html. I have a validation line which checks of all fields have been filled in and returns a error line if you missed something out. But when i fill in all the fields and click submit it returns this error to me. This is what makes me think it may be a typinhg error in the book. Here is the script anyway and i have highlighted the line causing the problems.

 

 

<?php

// Create short variable names

$isbn=$_POST['isbn'];

$author=$_POST['author'];

$title=$_POST['title'];

$price=$_POST['price'];

 

if (!$isbn || !$author || !$title || !$price)

{

echo ' you have not entered all the required fields.<br />'

                .'Please go back and try again.';

exit;

}

if (!get_magic_quotes_gpc())

{

$isbn = addslashes($isbn);

$author = addslashes($author);

$title = addslashes($title);

$price = doubleval($price);

}

 

@ $db = new mysql( XXXXX', 'XXXX', 'XXXXX', 'XXXX');

 

if (mysql_connect_errno ())

{

echo 'Error: Could not connect to database. Please check your settings and try again.';

exit;

}

 

$query = "insert into books values

('".$isbn."', '".$author."', '".$title."', '".$price."')";

$result = $db->query($query);

echo $db->affected_rows. 'book inserted into database.';

 

$db->close();

?>

 

Hope you can help me

 

Cheers

RichG

Link to comment
Share on other sites

$price=$_POST['price'];

 

 

Your text field on your HTML is "Price" so what he meant by lowercase was change the INPUT FIELD to lowercase. You have

 

 <input name="Price" type="text" id="Price">

 

Change it to

 

 <input name="price" type="text" id="price">

 

Also, you should really do away with get_magic_quotes_gpc() as it will be gone in PHP6 which is soon to release even though all servers won't have it right at the moment, it'd be a good idea.

Link to comment
Share on other sites

Always try to set your variables into an isset or empty function to check if they are 0,1/false,true otherwise you will return an error saying some crap about an undefined error.

 

<?php

if( empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['Price']) ) //Don't foget about case sensativity
{
   die(" you have not entered all the required fields Please go back and try again.");
}

if (!get_magic_quotes_gpc())
{
   $isbn = addslashes($_POST['isbn']);
   $author = addslashes($_POST['author']);
   $title = addslashes($_POST['title']);
   $price = doubleval($_POST['Price']);
}
     
@$db = new mysql('XXXXX', 'XXXX', 'XXXXX', 'XXXX');
if(mysql_connect_errno())
{
   die("Error: Could not connect to database. Please check your settings and try again.");
}

$query = "INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')";
$result = $db->query($query);
echo $db->affected_rows."book inserted into database.";
$db->close();

?>

Link to comment
Share on other sites

sidenote to current topic:

In php6 (if and when it arrives) the Engine will throw a E_CORE_ERROR on startup and should point you to the documentation.

Apparently they're going to change magic_quotes_* stuff for an extension called input_filter. So i imagine you'll get classes called $filter = new input_filter_string($string);

Link to comment
Share on other sites

Cheers guys and Chris 92. Removed all files from server and made sure caps were not there. Then copied Chris92's text in and it works when i miss out a field.

 

Can I confirm that the 'die' comment works the same as having an 'exit' at the end of an 'echo'?

 

Could you look at my '@ $db' line now please as I get niether the error line saying "could not connect to db" or the other one 'confirming my submission'. I have gone into mysql and the fields have not been submitted.

 

Cheers

RichG

Link to comment
Share on other sites

What is @$db = new MySQL(....) ?

Have you written a MySQL class? Do it connect to the database?

 

I would have expected the following instead:

@$db = mysql_connect('hostname', 'username', 'password);
mysql_select_db("database name");
if(mysql_connect_errno())
{
    die("Error: Could not connect to database. Please check your settings and try again.");
}

Link to comment
Share on other sites

I would use the mysql_connect funstion to connect to the databse, then use the mysql_select_db function to select the database, rather than the way you are doing it:

 

<?php

if( empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['Price']) ) //Don't foget about case sensativity
{
   die(" you have not entered all the required fields Please go back and try again.");
}

if (!get_magic_quotes_gpc())
{
   $isbn = addslashes($_POST['isbn']);
   $author = addslashes($_POST['author']);
   $title = addslashes($_POST['title']);
   $price = doubleval($_POST['Price']);
}

$connect = mysql_connect('host:port', 'username', 'password')  
if( empty($connect) )
{
   die("Error: Could not connect to mysql server.");
}

if( !mysql_select_db('database') )
{
   die("Error: Could not select the specifeid database.");
}

mysql_query("INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')");
printf(mysql_affected_rows(), "book inserted into database.");
mysql_close($connect);

?>

Link to comment
Share on other sites

Chris92

 

copied your code in and got this error

 

Parse error: syntax error, unexpected T_IF in /homepages/12/d235019564/htdocs/phptest/insert_book.php on line 30

 

What version of mysql should i have to be compatable with your code?

 

 

Link to comment
Share on other sites

right we are getting somewhere line by line lol.

 

This book may have to go in the bin as it is a couple of years old and i am out of practice. You are all typing different things now.

 

I now get one of the errors up which i am happy about as it means my code os working. I am now getting the cannot connect to server error. my password and username are right but i am not sure if my host:port is ok. i know my host name but do not have a port no. also should my host name be absolute ie http:// in front of it. Also my account says i have to access the db from within the server so need to get a loaclhost connection.

Link to comment
Share on other sites

Ok I now have entries into the databse and the connection works fine.

 

Just one last problem

 

When the form is submitted all the data goes to the db but the screen just shows the no 1 in the corner. go to http://www.richdgrimes.co.uk/phptest/newbook.html and see for yourself.

 

Here is the last bit of code

 

mysql_query("INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')");

 

printf(mysql_affected_rows(), "book inserted into database.");

 

Cheers

RichG

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.