7 Replies - 395 Views - Last Post: 13 April 2013 - 03:38 AM

#1 theplan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 13

Very simple PHP 500 error

Posted 10 April 2013 - 11:43 AM

Having a problem with a very simple script I'm using to add a forms fields to my database. I know that this method isn't secure, vulnerable to injections and the like, but I don't want to be lectured on that, I just want to know what is stopping my script from inserting the data. Whether it is a problem with the code or syntax, or whether it's a problem like getting the table names wrong (ie. if you think my script should work, it's probably a stupid error on my part!)

Thanks for taking the time to have a look!

<?php
//LINK WITH DBINFO.PHP
$host="blah"; 
$username="blah"; 
$password="blah"; 
$db_name="blah"; 
$tbl_name="blah";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$species=$_POST['species'];
$site=$_POST['site'];
$gridref=$_POST['gridref'];
$quantity=$_POST['quantity'];
$date=$_POST['date'];
$method=$_POST['method'];
$comments=$_POST['comments'];
$lat=$_POST['lat'];
$long=$_POST['long'];

$regex = "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/";
if(!preg_match($regex, $date))
{
  $errors[] = "The date is not in the correct format - YYYY/MM/DD";
}

if(!$species || !$site || !$quantity || !$date || !$method || !$lat || !$long)
{
   $errors[] = "The fields Site, Number, Date, Method, Lat and Long have to be filled to make a submission.";
}

$sql="INSERT INTO sightings (vernacular, site, gridref, quantity, date, method, comments, lat, long) VALUES
				('$species', '$site', '$gridref', '$quantity', '$date', '$method', '$comments', '$lat', '$long')";
$result=mysql_query($sql);

if($result){
echo "Success! The following addition was successful";
echo "<BR>";
echo "Species: <b>$species</b>";
echo "<BR>";
echo "Site:  <b>$site</b>";
echo "<BR>";
echo "Grid Ref:  <b>$gridref</b>";
echo "<BR>";
echo "Number:  <b>$quantity</b>";
echo "<BR>";
echo "Date:  <b>$date</b>";
echo "<BR>";
echo "Method:  <b>$method</b>";
echo "<BR>";
echo "Comments:  <b>$comments</b>";
echo "<BR>";
echo "Latitude:  <b>$lat</b>";
echo "<BR>";
echo "Longitude:  <b>$long</b>";
echo "<BR>";
echo "<a href='blah'>Please click here to return to the homepage -></a>";
}

else {
echo "ERROR - There was a problem adding your entry to the database. Please check your fields.";
}

mysql_close();
?>


I've been looking at this silly thing for hours now, and have made zero progress :( Please enlighten me!!!

Thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: Very simple PHP 500 error

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Very simple PHP 500 error

Posted 10 April 2013 - 11:49 AM

Instead of just printing out a generic error message if $result is false, why not use mysql_error() and see what the error message is. It could be anything from missing one of your variables having a value, conflicting key constraint, wrong data type, wrong database name, misspelling causing a syntax issue etc. The error message you see in mysql_error can tell us more about what is going on.

:)
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Very simple PHP 500 error

Posted 10 April 2013 - 11:54 AM

In the meantime, while date is not technically a MySql reserved word, long is. Surround both field names with `back-ticks` or, preferably, rename these fields.

Whilst, technically, MySql will allow you to use date as a field-name I, personally, wouldn't. Besides, date doesn't mean much on its own: is it a start_date, purchase_date, expiry_date??

This post has been edited by andrewsw: 10 April 2013 - 11:58 AM

Was This Post Helpful? 1
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Very simple PHP 500 error

Posted 10 April 2013 - 12:32 PM

If you still have no luck with any of that, it is time for you to start commenting things out until the error goes away. Then you will have a better idea what piece of code might be causing the error. Sometimes you get 500 errors if the PHP script just craps out on bad syntax. Also you can consult some kind of error log if your host has them.

:)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Very simple PHP 500 error

Posted 10 April 2013 - 12:46 PM

Your regex is also inconsistent with your error message:

$regex = "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/";
if(!preg_match($regex, $date))
{
  $errors[] = "The date is not in the correct format - YYYY/MM/DD";
}

..but anyway, display the sql error-message(s).
Was This Post Helpful? 0
  • +
  • -

#6 theplan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 13

Re: Very simple PHP 500 error

Posted 11 April 2013 - 07:00 AM

Thanks for all the helpful replies guys. I have sorted the problem, fixed my var names and about to fix the regex. The main issue was my insert statement. My database wanted backtiks `` around the column and table names, as well as some other little issues.

Now I have another quick question that will hopefully bring an end to this thread. I can now insert data in to the table, and as you can see the success message is the multiple HTML echos. I want to use the HTML for my sites to display the success message in rather than the plain text I have.

What is the best way to display this page while still being able to print out the POST vars?

Thanks very much!!!
Was This Post Helpful? 0
  • +
  • -

#7 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: Very simple PHP 500 error

Posted 12 April 2013 - 07:10 PM

View Posttheplan, on 11 April 2013 - 07:00 AM, said:

Thanks for all the helpful replies guys.

Thanks very much!!!



They help'd you to solve some of your problems then it would be nice if you pay them back by pressing the rep button or something. :) just a suggestion/advice , it might also help you someday.

This post has been edited by Syfer: 12 April 2013 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Very simple PHP 500 error

Posted 13 April 2013 - 03:38 AM

View Posttheplan, on 11 April 2013 - 02:00 PM, said:

Now I have another quick question that will hopefully bring an end to this thread. I can now insert data in to the table, and as you can see the success message is the multiple HTML echos. I want to use the HTML for my sites to display the success message in rather than the plain text I have.

What is the best way to display this page while still being able to print out the POST vars?

Thanks very much!!!

You have an $errors array, so you could echo these errors at appropriate points in your page, enclosed in a HTML-tag.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1