0

I'm trying to connect a HTML page using PHP form to MySQL database using Xampp. The HTML page is working fine but as soon as I press submit button a page with distinct characters appears, can anybody help with the problem?![php page][1]

HTML code:

    <html>
    <body>
    <body bgcolor="black">
    <font color="white">

    <form action="a.php" method="post">

    <center>
    <h1><font color = "gold"><marquee>Welcome to online bus booking!!!</h1></marquee>               </font color>
    <h3> 
    First Name:<br><input type = "text" name = "fnm"><br>
    Last name:<br><input type = "text" name = "lnm"><br>
    Mobile:<br><input type = "text" name = "mob"><br> 
    Age:<br><input type = "number" name = "age"><br>
    Source:<input type = "text" name = "src">
    Destination:<input type = "text" name = "des"><br>
    <font color="orange">Passenger Address:<br></font color>
    Street:<br><input type = "text" name = "str"><br>
    Area:<br><input type = "text" name = "area"><br>
    City:<br><input type = "text" name = "cty"><br>

    <input type = "submit">

    </h3>
    </center>
    </form>

    </body>
    </html>

PHP code:

    <?php
    $con=mysqli_connect("localhost","root","","testdb");
    if(mysqli_connect_error())
   {
       echo "FAILED" . mysqli_connect_error();
   }
   $sql="INSERT INTO exp VALUES('$_POST[fnm]', '$_POST[lnm]', $_POST[mob], $_POST[age],        '$_POST[src]', '$_POST[des]', '$_POST[str]', '$_POST[area]', '$_POST[cty]')";
   if (!mysqli_query($con,$sql))
   {
       die('Error: ' . mysqli_error($con));
   }
   echo "1 record added";
   mysqli_close($con);
   ?> 
4
  • Where is your php code? Commented Oct 4, 2014 at 2:26
  • What is 'a page with distinct characters'? Commented Oct 4, 2014 at 2:28
  • @iatboy I've added the php code Commented Oct 4, 2014 at 2:32
  • @MikeW a page with "PK!Ýü•7f [Content_Types].xml ¢( ´TËnÂ0¼Wê?D¾V‰¡‡ªªú8¶H¥ì XõKöòúûnDUA*å)YïÌììăÑÚšl 1iïJÖ/z,'½ÒnV²ÉK~ϲ„Â)a¼ƒ’m ±Ñðúj0ÙHu»T²9bxà<É9X�? ÀQ¥òÑ ¤×8ãAÈO1~ÛëÝqé‚Ãk6$N{9›êÍ+P9Y ¢†vuÇGD²ìÃï»ÆoR€”wàͳ¶ ÌIÊŠ~‰‰˜8›ïWòZè“"V0}¿˜ûßÀ»„´ù“>þÁŒýuQwHoî·áÿÿPK!�?·óN_rels/.rels ¢( Œ’ÛJA†ïßaÈ}7Û "ÒÙÞH¡w"ë„™ìw̤ھ½£ ºPÛ^æôçËOÖ›ƒ›Ô;§<¯aYÕ Ø›Gßkxm·‹PYÈ[š‚g GΰinoÖ/<�?”¡<Œ1«¢â³†A$>"f3°" and so on Commented Oct 4, 2014 at 2:33

4 Answers 4

3

Here, let me correct the syntax :

$sql="INSERT INTO exp VALUES('".$_POST['fnm']."','".$_POST['lnm']."','".$_POST['mob']."', '".$_POST['age']."','".$_POST['src']."','".$_POST['des']."','".$_POST['str']."','".$_POST['area']."', '".$_POST['cty']."')";
0
0

this code is wrong : $sql="INSERT INTO exp VALUES('$_POST[fnm]', '$_POST[lnm]', $_POST[mob], $_POST[age], '$_POST[src]', '$_POST[des]', '$_POST[str]', '$_POST[area]', '$_POST[cty]')";

as you are not referring $_POST variables using their name , missing the quotes surrounding the variable name. correct the line and your data should be inserting successfully.

1
  • Nope, it is showing same error page, can you be more specific with the correction? Commented Oct 4, 2014 at 2:43
0

Try using the following PHP and let us know what happens:

$sql="INSERT INTO `exp` VALUES('{$_POST['fnm']}', '{$_POST['lnm']}',
    '{$_POST['mob']}', '{$_POST['age']}', '{$_POST['src']}', '{$_POST['des']}',
    '{$_POST['str']}', '{$_POST['area']}', '{$_POST['cty']')"
    or die('Error: ' . mysqli_error($con));
$output = $con->$sql
echo "1 record added";
mysqli_close($con);

Is anything different this way?

Note: To be syntactically proper you would want to explicitly state where you want the values inserted as follows:

$sql="INSERT INTO `table_name` (`first_value`, `second_value`, `third_value`)
    VALUES ('{value1}', '{value2}', '{value3}');";
0

I know it's way too late guys, but I solved the issue. This doesn't means it took me 3 years to solve it, I just forgot to post the answer. It was happening because I was editing the php code in Wordpad. I copied and saved the code in Notepad and the issue was solved.

Thank you for your valuable input guys.

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.