Jump to content

Basic insesrt questions


Recommended Posts

As I am reading through the various samples I some times see mysql_query and sometimes @mysql_query is one preferred over the other?Also I am having trouble with the following code.Server side is:

<?$username = "root";$password = "";$database="sample";$con = mysql_connect(localhost,$username,$password) or die("connect error: ".mysql_error() );mysql_select_db($database,$con) or die( "Unable to select database"); $first = $_POST['first'];$last = $_POST['last'];$phone = $_POST['phone'];$mobile = $_POST['mobile'];$fax = $_POST['fax'];$email = $_POST['email'];$web = $_POST['web'];echo ("Last name is ".$last);$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";mysql_query($query, $con) or die("insert failed: ".mysql_error());echo "Insertion succeeded";mysql_close();?> 

and browser side is :

<form action="insert.php" method="post">First Name: <input type="text" value = "Fred" name="first"><br>Last Name: <input type="text" value = "Jones" name="last"><br>Phone: <input type="text" name="phone"><br>Mobile: <input type="text" name="mobile"><br>Fax: <input type="text" name="fax"><br>E-mail: <input type="text" name="email"><br>Web: <input type="text" name="web"><br><input type="Submit"></form>

Nothing is written back to the browser. No "die" messages nor "echo"sWhat did I miss.The database and the table have been created.

Link to comment
Share on other sites

The @ operator suppresses error messages from being outputted if the function caused an error. It's generally a bad practice to suppress errors, if an error happens you want to be aware of it so you can fix it. It's a much better practice to use error logging to send all error messages to a file. That way, users don't see the error messages in the browser and you can always go in and check the error log to see a record of everything that has happened. You can put this code in your script to send all errors to a file called error.log in the same directory as the script:error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);If you're not seeing errors then they might not be enabled. Include the code above in your form processing script, and then check the error log (or change those settings to display errors instead of log them). If you're still not seeing any output, use a parent file to set all of the options first, then include the file to test, e.g.:

<?phperror_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1);include 'file.php';?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...