Question:

Why does my php code does not update my MySQL database?

by  |  earlier

0 LIKES UnLike

Why does my php code does not update my MySQL database?

Here is my code

<?php

if ($HTTP_POST_VARS["name"] == "" || $HTTP_POST_VARS["course"] == "NONE" ||

$HTTP_POST_VARS["year"] == "NONE" || $HTTP_POST_VARS["idnumber"] == "")

{

echo "<h3> Error! You have not entered all required details. Pleaes try again. </h3>";

echo "<a href=' /new/csitorg.html'> Go back to Main page</a>";

}

else

{

echo "<p> Payment is made by <br>";

echo $HTTP_POST_VARS["idnumber"]. '<br>';

echo $HTTP_POST_VARS["name"]. '<br>';

echo $HTTP_POST_VARS["course"].' ';

echo $HTTP_POST_VARS["year"]. '<br>';

$idnumber = addslashes($HTTP_POST_VARS["idnumber"]);

$name = addslashes($HTTP_POST_VARS["name"]);

$course = addslashes($HTTP_POST_VARS["course"]);

$yearr = addslashes($HTTP_POST_VARS["year"]);

$firstsemfee = 0;

$secondsemfee = 0;

$shirtfee = 0;

$acqfee = 0;

@ $db = mysql_pconnect("localhost","root",...

..."mysql");

echo $db;

if(!$db)

{

echo "Error: Could not connect to database. Please try again later.";

exit;

}

mysql_select_db("csitorg");

$query = "insert into students values ('".$idnumber."', '".$name."','".$course."','".year."','"....

$result = mysql_query($query);

echo $query;

echo $result;

?>

all of the other things are already checked. May asked is there something that i need to do? Something to add in my code? I used XAMPP as a server and MySQL 5.

This is my table in MySQL Database

-------------- ------------ ------ ----- --------- -------

| Field | Type | Null | Key | Default | Extra |

-------------- ------------ ------ ----- --------- -------

| idnumber | char(6) | NO | PRI | NULL | |

| name | char(30) | NO | | NULL | |

| course | char(6) | NO | | NULL | |

| year | int(1) | NO | | NULL | |

| firstsemfee | tinyint(1) | NO | | NULL | |

| secondsemfee | tinyint(1) | NO | | NULL | |

| shirtfee | tinyint(1) | NO | | NULL | |

| acqfee | tinyint(1) | NO | | NULL | |

-------------- ------------ ------ ----- --------- -------

THANK YOU VERY MUCH!

 Tags:

   Report

4 ANSWERS


  1. Not checking anything else, your INSERT query is incorrect!

    $sql = insert into `tablename` ( `field1` , `field2` , ... , `fieldN ) values ( &#039; &quot; . $field_1 . &quot; &#039; , &#039; &quot; . $field_2 . &quot; &#039; , .... , &#039; &quot;. $field_N &quot; . &#039; )

    mysql_query ( $sql )

    if ( mysql_error ( ) )

    echo ( &quot;sql: &quot; . $sql . &quot;
    &quot; . mysql_error ( ) ) ;


  2. Take off &quot;echo $result;&quot; and put this instead:

    if (!$result) {

    echo &#039;An error has occurred: &#039; . mysql_error();

    }

    That should give you the error to why it isn&#039;t working correctly.

    If you need further help, IM me on Yahoo.

  3. Just browsing through your code everything seems correct. The best way to check if your query is correct or not is to go the database directly and execute your query there.

    Since its mysql- you can use phpmyadmin and if you have hosted your XAMPP on localhost then the URL is http://localhost/phpmyadmin/

    Even if this is on some host, then you can go the phpmyadmin from cpanel and then try your query there.


  4. Have you checked to make sure that the account you are connecting to the server as has INSERT/UPDATE/DELETE permissions?

    It looks as though you are connecting as &quot;root&quot; which should have the appropriate rights, but it might be worth looking into.

    Good luck : )

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.