Help Me Debug This Piece of Code Please!

Discussion in 'PHP' started by fireflyproject, Jun 11, 2008.

  1. #1
    $user = $_SESSION['email'];
    	$time = time();
    	$name = $_POST['name'];
    	$address = $_POST['address'];
    	$city = $_POST['city'];
    	$state = $_POST['state'];
    	$zip = $_POST['zip'];
    	$owner = $_POST['owner'];
    	$price = $_POST['price'];
    	$phone = $_POST['phone'];
    	$features = $_POST['features'];
    	$type = $_POST['type'];
    	//$image = $_POST['image'];
    	$image = "1";
    
    	if ($name != "") {
    		$sql = "insert into biz_4_sale values ('', '$ip', '$user', '$time', '$name', '$address', '$city', '$state', '$zip', '$owner', '$price', '$phone', '$features', '$type', '$image', '0')";
    		mysql_query($sql);
    		
    		$sql = "select * from biz_4_sale where ip = '$ip' and time = '$time'";
    		$result = mysql_query($sql);
    		$row = mysql_fetch_array($result);
    		$id = $row['id'];
    	}
    PHP:
    I'm sure there's a better way to do this, but this is just a quick little piece I need working pretty fast.

    The problem: It's not getting inserted into the database. I believe it was a few days ago when I wrote it, but now that I'm going through to optimize my code and test it all, this piece isn't working. Any idea why?

    Thanks!!!
     
    fireflyproject, Jun 11, 2008 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    Just add or die(mysql_error()) to debug it.

    mysql_query($sql) or die(mysql_error());

    Now, let us know what the error message is.
     
    Lordo, Jun 11, 2008 IP
    fireflyproject likes this.
  3. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Append ' or die(mysql_error)' to yourt queries and tell me what error it returns
    
    $result = mysql_query($sql) or die(mysql_error);
    
    PHP:
     
    rohan_shenoy, Jun 11, 2008 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Also check that your are inserting exactly that many fields as the row can hold. not a single less or more!
     
    rohan_shenoy, Jun 11, 2008 IP
  5. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Ah Jesus. User error.

    Thanks Lordo.

    I had a single quote screwing things up. I need to switch those out with the hex value. I guess I'll go ahead and do the security bit now too then!
     
    fireflyproject, Jun 11, 2008 IP
  6. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    ^to avoid those error due to single quotes always use mysql_real_escape_string() function! It will take care of single as well as double quotes.
     
    rohan_shenoy, Jun 11, 2008 IP
    fireflyproject likes this.
  7. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Oh awesome! I was just going to use str_replace on all my variables... this will take just a bit of typing away. Cool, thanks. +rep
     
    fireflyproject, Jun 11, 2008 IP
  8. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    ^Welcome :)
     
    rohan_shenoy, Jun 11, 2008 IP