• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SQL/PHP. I could do with some advice on adding records.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

grs

Member
Joined
Apr 3, 2006
Location
Dublin, Ireland
I'm trying to use a SQL database and PHP webpages to create a catalogue of my book collection. So far I have a DB with a table, and I have some HTML/PHP pages that allow me to add records to the DB via a form.

I want to be informed if a record already exsists and have the option of actully creating two similar records with an OK or Cancel button.

I want to be informed if I try adding a record with all fields empty, this is to prevent accidentally adding blank records. I don't mind if all but one field are empty.

Trying to do this might be beyond my current understanding!

Here is the code I'm using, the insert file where the data is input (there is a bit more html code to colour etc. to the page):-

Code:
<html>
<body>

<form action="insert.php" method="post">
Title: <input type="text" name="title" />
Author: <input type="text" name="author" />
Year: <input type="text" name="year" />
<input type="submit" />
</form>

</body>
</html>

The insert file with the action has been confirmed:-

Code:
<?php
$con = mysql_connect("192.168.1.110","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lib", $con);

$sql="INSERT INTO lic (Title, Author, Year)
VALUES
('$_POST[title]','$_POST[author]','$_POST[year]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
 
I want to be informed if a record already exsists and have the option of actully creating two similar records with an OK or Cancel button.
How do you know if a record already exists, checking based on title, author, or year?

For the other bit, checking if all fields are blank:
Code:
<?php
if(($_POST['title'] == '') && ($_POST['author'] == '') && ($_POST['year'] == ''){
//The above line assumes that year is being entered through a text field.  If it is a dropdown box, you'll need to replace '' with the default value that is passed through.
$con = mysql_connect("192.168.1.110","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lib", $con);

$sql="INSERT INTO lic (Title, Author, Year)
VALUES
('$_POST[title]','$_POST[author]','$_POST[year]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
}else{
  echo 'All fields are blank.';
}
?>
 
I suppose checking pre exsisting titles is best as the chances of my having several books by an author are higher than the chances of me having several books of the same title.

For the blank field part, I had the year set as a Year field but changed it to a Text for this purpose.
There is something not right with it, when I click to submit with the record blank or filled in I get a blank page returned. I tried altering a few sections but nothing worked, and I did remember to put in the username and password.
 
Back