Solved

php form problem, cant insert new record to table

Posted on 2009-07-02
16
222 Views
Last Modified: 2013-12-13
hey everyone
im  kinda new to php and mysql and im having trouble inserting new record in to my database table. im not throwing the php code in the same html page. so i have the form in adduser.html then when user add all fields and submit it will go to adduser.php
in adduser.php i was able to check all inputs but cant send them to the database....
any ideas?


### ------HTML FORM--------- ###

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

First name:<br>

<input type="text" name="fname"><br>

Last name:<br> 

<input type="text" name="lname"><br>

Email:<br> 

<input type="text" name="email"><br>

Password:<br> 

<input type="password" name="password"><br>

School/University:<br>

<input type="text" name="school_name"><br>

City:<br> 

<input type="text" name="city"><br>

State:<br> 

<input type="text" name="state">
 

<INPUT type="submit" value="send"><INPUT type="reset">
 

###----- PHP-----###
 

<?php

include 'config.php';      

include 'opendb.php';

/* Check all form inputs using check_input function */

$fname        = check_input($_POST['fname'], "Enter your first name.");

$lname        = check_input($_POST['lname'], "Enter your last name.");

$password     = check_input($_POST['password'], "Enter your pass.");

$email        = check_input($_POST['email'], "Enter your Email.");

$school_name  = check_input($_POST['school_name'], "Enter your School Name.");

$city         = check_input($_POST['city'], "Enter your City.");

$state        = check_input($_POST['state'], "Enter your State.");
 
 

/* If e-mail is not valid show error message */

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))

{

    show_error("E-mail address not valid");

}

else{

$data = "INSERT INTO users VALUES ('NULL', '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";

$query = mysql_query($data);

  if(!$query)

  {

     die(mysql_error());

  }

  else

  {

    

    /* Redirect page */

	/*header('Location: thanks.html');*/
 
 

  } 

echo "<TD valign='top'><br /><center><b>You successed!";

echo "<br>";

}
 
 
 

/* Functions I used to check */

function check_input($data, $problem='')

{

    $data = trim($data);

    $data = stripslashes($data);

    $data = htmlspecialchars($data);

    if ($problem && strlen($data) == 0)

    {

        show_error($problem);

    }

    return $data;

}
 

function show_error($myError)

{

?>

    <html>

    <body>
 

    <b>Please correct the following error:</b><br />

    <?php echo $myError; ?>
 

    </body>

    </html>

<?php

exit();

}

?>

Open in new window

0
Comment
Question by:colin0292
  • 6
  • 6
  • 3
  • +1
16 Comments
 
LVL 5

Expert Comment

by:Opalelement
ID: 24769932
Is it returning any mysql error?
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 24769940
Try to print the Insert query and check the syntax.

$data = "INSERT INTO users VALUES ('NULL', '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";
 

echo $data;
 

$query = mysql_query($data);

Open in new window

0
 

Author Comment

by:colin0292
ID: 24769990
Opalelement it did not return any error
0
 

Author Comment

by:colin0292
ID: 24769994
logudotcom it didn't echo $data
0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24770004
It would have to echo $data... Are you sure you put it in the right spot?

A test URL would be good:)
0
 

Author Comment

by:colin0292
ID: 24770031
i think i did put it in the right spot


  } 

echo "<TD valign='top'><br /><center><b>You successed!";
 

echo $data;
 

echo "<br>";

}

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
ID: 24770043
you can try

$data = "INSERT INTO users (ffield, fname, lname, password, email, school_name, city, state) VALUES ('NULL', '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";
0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24770060
It goes between lines 41 and 42... Between these:
$data = "INSERT INTO users VALUES ('NULL', '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";

$query = mysql_query($data);

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:colin0292
ID: 24770071
didn't work fsze88
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24770082
will it caused by missing database link?
$link = mysql_connect('localhost', 'username', 'password');
mysql_query( $data, $link );
??
0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24770084
There would be an error if it wasn't connected to the database. With mysql_query, you don't need to pass the link.
0
 

Author Comment

by:colin0292
ID: 24770105
Opalelement now it echos $data but still not to database
and about my connection
i have two includes

include 'config.php';      
include 'opendb.php';
see the code below..

============config.php===========

<?php

// This is an example of config.php

$dbhost = 'localhost';

$dbuser = 'colin';

$dbpass = '*****';

$dbname = 'rating';

?>
 
 

============opendb.php==============

<?php

// This is an example opendb.php

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');

mysql_select_db($dbname);

?>

Open in new window

0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24770109
Can you copy and paste the $data that was echoed?
0
 

Author Comment

by:colin0292
ID: 24770123
Opalelement i think you got this but what should i do with the null thing since the user_id is set to be auto_inc in the table


INSERT INTO users VALUES ('NULL', 'aliiiiiiiiiiiiii', 'rtyu', '123456', 'fghj@dfgh.com', 'dfghjkl;', 'ertyuio', 'cv')Incorrect integer value: 'NULL' for column 'user_id' at row 1
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24770134
you can try omit insert id field

$data = "INSERT INTO users ( fname, lname, password, email, school_name, city, state) VALUES ( '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";

0
 
LVL 5

Accepted Solution

by:
Opalelement earned 125 total points
ID: 24770136
Take null out of single quotes. That makes it so you are passing the word "null", not a null value. So it should be:
$data = "INSERT INTO users VALUES (NULL, '$fname', '$lname', '$password', '$email', '$school_name', '$city', '$state')";

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now