-3

i have problems inserting data, it does not enter any data to my phpmyadmin im having problems

<?php
if (isset($_POST['submit'])){
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$MiddleInitial=$_POST['MiddleInitial'];
$Age=$_POST['Age'];
$Gender=$_POST['Gender'];
$Birthday=$_POST['Birthday'];
$Address=$_POST['Address'];
$EmailAddress=$_POST['EmailAddress'];

mysql_query("insert into studentsrecords(LastName,FirstName,MiddleInitial,Age,Gender,Birthday,Address,EmailAddress)
values('$LastName','$$FirstName','$MiddleInitial','$Age','$Gender','$Birthday','$Address','$EmailAddress',NOW())
")or die(mysql_error());
?>

<script type="text/javascript">
alert('You are Successfully Register Thank You');
window.location="index.php";
</script>

<?php
}
?>

this is my config.php

cimmanon
  • 67,211
  • 17
  • 165
  • 171
  • and where is your `mysql_connect` function? – antyrat Oct 30 '13 at 18:45
  • [mysql_query](http://www.php.net/manual/tr/function.mysql-query.php) _Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used._ – Andreas Oct 30 '13 at 18:48
  • What does this have to do with phpMyAdmin? – Quentin Oct 30 '13 at 18:48
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 30 '13 at 18:48
  • For one thing, you have more values than your INSERT. I.e.: `NOW()` – Funk Forty Niner Oct 30 '13 at 18:53
  • huh?what if i remove now()? – Vincent Reguindin Oct 30 '13 at 18:56
  • @VincentReguindin It will balance things out, yet if you don't have a column for the time, then yes, do delete it. Entries need to match in numbers, otherwise your query will fail. – Funk Forty Niner Oct 30 '13 at 18:57
  • @VincentReguindin [Shankar's answer covers it all](http://stackoverflow.com/a/19691332/1415724) – Funk Forty Niner Oct 30 '13 at 18:59
  • i'd remove it.,.,nothing happens – Vincent Reguindin Oct 30 '13 at 19:02
  • @VincentReguindin Did you follow Shankar's answer/recommendations below? And double check your column names etc. spelling/letter-case, it makes a difference having `firstname` and `FirstName` if that's the case. – Funk Forty Niner Oct 30 '13 at 19:02
  • @VincentReguindin Also make sure you also deleted the last comma in `'$EmailAddress',NOW())` to now read as `'$EmailAddress'` as the last entry. The recommendations SHOULD work. If not, then there's something wrong elsewhere. – Funk Forty Niner Oct 30 '13 at 19:04
  • @VincentReguindin Try this `$con = mysql_connect("localhost","mysql_user","mysql_pwd");` <= being your DB info, then `$sql = "insert into studentsrecords(LastName,FirstName,MiddleInitial,Age,Gender,Birthday,Address,EmailAddress) values ('$LastName','$$FirstName','$MiddleInitial','$Age','$Gender','$Birthday','$Address','$EmailAddress')";` then under that `mysql_query($sql,$con);` – Funk Forty Niner Oct 30 '13 at 19:14

2 Answers2

3

Some common mistakes on your code. Fix these up.

  • Change '$$FirstName' to '$FirstName' in your mysql query.

  • You don't have a connection established to your DB. Proposed by antyrat

  • Remove the NOW() from your mysql query. Proposed by andrewsi

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

replace '$$FirstName' to '$FirstName' check your form method is "post" or not, check your configuration file. you may create database connection by following method

$user = "xxxx"; //Database Username here
    $pass = "xxxx"; //Database Password here
    $db = "xxxx"; //Database Name here
    $localhost = "xxx"; //Database Server


$link = mysql_pconnect($localhost, $user, $pass);
if ( ! $link )
    die( "Couldn't connect to MySQL" );
mysql_select_db( $db)
    or die ( "Couldn't open $db: ".mysql_error() );
Tousif
  • 301
  • 3
  • 11