0

I am not able to connect my php code with the mysql database in localhost.Here is my php code:

  <? php
     echo "hi";
     $con=mysql_connect("localhost","root","*********");
     if(!$con)
     {
          die('could not connect'.mysql_error());
     }
     mysql_select_db("my_db",$con);
     mysql_query("insert into my_table(firstname,idnumber) values ('$_post[firstname]','$_post[idnumber]')");
     echo "'hi ur name is ('$_post[firstname]')";
     mysql_close($con);
     ?>

What should i do? and also when i am running the php code it is also not echoing the "hi" statement {echo "hi"}. Is that a connection problem?


I got the result. Now whenever am entering the data in the form,the table shows a row is updated but the fields have 0..not the entered data.

5
  • 1
    Also, google for 'sql injection' - your code is vulnerable to it and you'd might as well learn to avoid issues like these. Commented Oct 24, 2011 at 6:57
  • 2
    As an aside also consider using PDO instead. You get a lot for free if you use them. Commented Oct 24, 2011 at 6:58
  • i just installed sql.I have no idea about PDO and 'sql injection'. Will they solve the connection problem? Commented Oct 24, 2011 at 7:09
  • Are you running this on a linux or a windows platform? Commented Oct 24, 2011 at 7:23
  • am running on fedora14 and using httpd... Commented Oct 24, 2011 at 9:53

4 Answers 4

3
<?php  //No space between ? and php

?>

Try,

<?php

 $con=mysql_connect("localhost","root","*********") or die(mysql_error());
 mysql_select_db("my_db",$con) or die(mysql_error());

 $result=mysql_query("insert into my_table (firstname,idnumber) values ('$_post[firstname]','$_post[idnumber]')");
 if($result)
    //success
 else
    //not success
?>
Sign up to request clarification or add additional context in comments.

1 Comment

That was the answer I was just about to give, but I refreshed the page right before :)
0

please check

short_open_tag = On in your php.ini

4 Comments

it was off. I chnaged it to on state.What is this usually used for?
when you make in "ON" you can use short tags like <? ?> and if it is OFF you have to write php tags in full tags like follows <?php ?>
just noticed in your code <? php there is a space between <? and php it cound be fatal error when runs on apache
That i just rectified.But same problem still prevails.
0

if you running on xampp. just weather your engine is running mysql or not, and check the syntax to be more clear Ex: mysql_query("insert into my_table(firstname,idnumber) values ('". $_post['firstname']."','.".$_post[idnumber]."')");

Like this.

Comments

0

Try<?php echo "Hello"?>, If this wont print hello then there is a problem with your PHP installed in your local machine. Also try this

<? php
 $con=mysql_connect("localhost","root","*********")  or die(mysql_error());
 if(!$con)
 {
      die('could not connect'.mysql_error());
 }
 mysql_select_db("my_db",$con);
 mysql_query("insert into my_table(firstname,idnumber) values ('Name','12')") or die(mysql_error());
 mysql_close($con);
 ?>

The above code should insert Name and id into your database table.

2 Comments

I guess there is a configuration issue.It is not echoing any string.Can you suggest me what to deal with that.
Well did it echo hello when u tried <?php echo "Hello"?>?? If not reinstall php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.