If you’re learning and new to PHP and possibly HTML, given that you’ve come from COBOL :D, there’s a couple of things I would suggest:
-
Forget mysql_query it’s old and will be dropped from future versions of PHP at some point, so you may as well get to grips with the newer alternatives now, rather than having to re-learn them in the future and possibly update sites that you’ve built. Take a look at mysqli: http://www.php.net/manual/en/intro.mysqli.php or PDO: http://www.php.net/manual/en/intro.pdo.php. I like PDO because it can be used to connect to several different databases, not just MySQL.
-
Have a look into how you split up the server side processing and the output to browser parts of your page/code. Typically purely server side code is placed above the HTML, with the exception of code that produces output, for example loops where you are displaying rows from a recordset, or array. For example:
[php]<?php
$errorMessage = ‘’;
if (isset($_POST[‘submit’])){
include ‘db.php’;//Connect to database
$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;
$num1=$_POST[‘numb1’];
$num2=$_POST[‘numb2’];
$title_search = mysql_query(“SELECT Title FROM books WHERE Title=’$title’ AND Author=’$author’”);
$tot_num = $_POST[‘numb1’] + $_POST[‘numb2’] * 100;
echo $tot_num;
if(mysql_num_rows($title_search) != 0) {
$errorMessage = ‘Record Already Exist!’;
}else {
mysql_query(“INSERT INTO books
(Title, Author, PublisherName, CopyrightYear) VALUES (’$title’, ‘$author’, ‘$name’, ‘$copy’)”);
}
}
mysql_close($conn);
?>
Books
<?php
if ($errorMessage != ''){echo $errorMessage;}
?>
Title: |
|
Author |
|
Publisher Name |
|
Copyright Year |
|
First Number |
|
Second Number |
|
|
|
<?php
include("db.php");
$result=mysql_query("SELECT * FROM books");
while($test = mysql_fetch_array($result)){
$id = $test['BookID'];
echo "";
echo"" .$test['BookID']." | ";
echo"" .$test['Title']." | ";
echo"". $test['Author']. " | ";
echo"". $test['PublisherName']. " | ";
echo"". $test['CopyrightYear']. " | ";
echo" Edit";
echo" |
Delete";
echo " |
";
}
mysql_close($conn);
?>
[/php]
- I noticed a couple of syntax errors in your code, you had an unpaired curly brace:
[php]<?php
if (isset($_POST[‘submit’])){
include ‘db.php’;//Connect to database
$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;
$num1=$_POST[‘numb1’];
$num2=$_POST[‘numb2’];
$title_search = mysql_query(“SELECT Title FROM books WHERE Title=’$title’ AND Author=’$author’”);
$tot_num = $_POST[‘numb1’] + $_POST[‘numb2’] * 100;
Echo $tot_num;
if(mysql_num_rows($title_search) != 0) {
echo 'Record Already Exist!';
} else {
mysql_query("INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) VALUES ('$title', '$author', '$name', '$copy')");
}
mysql_close($conn);
} // <-------------------------------------This one was missing
?>[/php]
Also you had: Echo $tot_num;, echo needs to be lower case.
What are you using to write the code? If you’re using Notepad or some other plain text editor you might want to try something like Notepad++ as this has syntax highlighting and also highlights pairs of curly braces and brackets. There are some reviews of and links to some free programming editors here: http://www.techsupportalert.com/best-free-programming-editor.htm