Inserting information into a table...

A

Anonymous

Guest
Code:
<?
include("config.php");
$conn = mysql_connect("$db_host", "$db_usr", "$db_pw");
mysql_query("INSERT INTO `members` ( `name` , `email` ,  `password` , `rank`)");
mysql_close($conn);
?>

What's wrong, there's no errors it just won't insert anything into the database. My form variables are
name="name"
namel="email"
name="password"
and rank as a default on the table
 
Well, to begin with, you need to tell it what values to put in the columns you specified. If you don't give it any values, what do you expect it to put in the row? Something like this:

Code:
<?php
// all of those quotation marks were extraneous
$conn = mysql_connect($db_host, $db_usr, $db_pw);

// all of those backtics are extraneous, too
$query = "INSERT INTO members (name, email, password, rank) " .
         "VALUES ('swirlee', 'swirlee@php-forum.com', MD5('somepassword'), 100)"

mysql_query($query);
?>
 
Yes but people input that information, so what do I do then? Do I put in the form variable in that spot?
 
HaVoC said:
Yes but people input that information, so what do I do then? Do I put in the form variable in that spot?

Yes.

Although you'll want to do some validation first.
 
That still doesn't work :(
No errors, it just won't insert the info
 
swirlee's code is fine!
is it giving you any errors
test your query on mysql then copy/modify it to php
 
please give the code and the structure of your table...

that would be helpful :)
 
<?
include("config.php");
$conn = mysql_connect($db_host, $db_usr, $db_pw);
mysql_query("INSERT INTO members (name, email, password, rank) " . "VALUES ('name', 'email', 'password', '13')");
?>

Rank is a defined value. The values are the names of the form inputs.
ex:
Code:
<input type="text" name="name">
 
HaVoC said:
<?
include("config.php");
$conn = mysql_connect($db_host, $db_usr, $db_pw);

mysql_select_db("your_database_name",$conn);

mysql_query("INSERT INTO members (name, email, password, rank) " . "VALUES ('name', 'email', 'password', '13')");
?>

Rank is a defined value. The values are the names of the form inputs.
ex:
Code:
<input type="text" name="name">

you have not selected the database!
 
Ok, I selected the database. No errors, still nothing. It can't be the values because NOTHING is being inserted into the database at all.
 
HaVoC, PHP doesn't automatically report problems with your MySQL queries, You have to use mysql_error() in order to find out if there were any problems with the execution query. It will also serve you well to write your queries in a MySQL client (e.g. MySQLCC or just the mysql.exe prompt) rather than trying to write them in PHP.

My guess (since we still haven't seen the structure of your table) as to your problem is that you're trying to put a string (e.g. '13', with quotes) rather than a number (e.g. 13, without quotes) into a numeric (e.g. INT) field.
 
Ok it works, but the values are all wrong. It's not inserting what was typed in on the form. This is the code

Code:
<?
/* Connecting, selecting database */
$link = mysql_connect("localhost", "sfsdf", "sfsdf") or die("Could not connect : " . mysql_error()); print "Connected successfully";
mysql_select_db("stsite_members") or die("Could not select database"); 
/* Performing SQL query */
$query = "INSERT INTO members (name, email, pw, rank) " . "VALUES ('name', 'email', MD5('pw'), 13)";

/* Inserting info into table */
mysql_query("$query");
/* Closing connection */
mysql_close($link);

?>

And what do you mean table structure, for future reference.
 
HaVoC said:
And what do you mean table structure, for future reference.

Go to the MySQL command line and type "describe members;". That's what I mean by table structure.

If the values are "all wrong", what are they?
 
I mean the values are name, email, ect. The password isn't right is give the MD5 value for pw. And those are the form variables >.<
 
Code:
mysql_query(your query here) or die(mysql_error());

check what error you get..
 
There is NO error! It just inserts the values name and email instead of the information the user actually inputted. >.<
 
HaVoC said:
It just inserts the values name and email instead of the information the user actually inputted. >.<

Code:
<? 
include("config.php"); 
$conn = mysql_connect($db_host, $db_usr, $db_pw) or die(mysql_error()); 

mysql_select_db("your_database_name",$conn)  or die(mysql_error()); ; 

mysql_query("INSERT INTO members (name, email, password, rank) " . "VALUES ('$name', '$email', '$password', '13')"); 
?> 
$email=the actual email inserted by user through form and get the variable by $_GET or $_POST
same for other
 
Back
Top