Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help...PHP Newbie

Status
Not open for further replies.

cnagra

Technical User
Apr 16, 2006
3
GB
hi,
please can someone help me, im new to php and have to create a stock control system
i been trying to add data to mysql database through php. i am having trouble with adding to the database when there is multiple fields. everything is fine when the database has one field, but if it has more than one, no data is added to it, not even the workinf original field. i dont know why...can someone help?

my form html - for when there is one field

<form name="form" method="post"
action="add_workin.php">

Name: <input type="text" name="name"><br>

<input type="Submit">
</form>

my php code for one field

<?php

mysql_pconnect('localhost');
mysql_select_db('test2');

mysql_query("insert into test values ('$name')");

?>

my html for 2 fields....(NOTHING GETS ADDED not even the working field)

<form name="form" method="post"
action="add_workin.php">

Name: <input type="text" name="name"><br>
Category: <input type="text" category="name"><br>

<input type="Submit">
</form>

my php code for two fields

<?php

mysql_pconnect('localhost');
mysql_select_db('test2');

mysql_query("insert into test values ('$name')");
mysql_query("insert into test values ('$category')");

?>


please help...thanks
 
The basic problem is your MySQL insert statement. If you have a table with more than one field but only send one value, it will just make a record with only the first field (in the order you created the table) having data. So in your example, you made two new records, but neither with the data you wanted. Also, it's good to actually specify in the insert statement which fields you want the data in, so that no matter what "order" the fields are in your table structure, the data will go in the right places. Try the following code (you haven't told us your database field names, but for my example I'll call them "NameField" and "CategoryField" - you can substitute the real names):
Code:
mysql_query("insert into test(NameField,CategoryField) values ('$name','$category')");
Also, one more recommendation: rather than depending on "register_globals" being on (if you haven't heard that term, don't worry, you will as you learn more, but for now, just trust me), I recommend accessing the variables from your form more explicitly, like this:
Code:
mysql_query("insert into test(NameField,CategoryField) values ('{$_POST['name']}','{$_POST['category']}')");

Welcome to the PHP/MySQL world!
 
it still not working, here is my code again

html file

<form name="form" method="post"
action="add_workin.php">

Name: <input type="text" name="name"><br>
Test: <input type="text" name="topic"><br>

<input type="Submit">
</form>

php file

<?php

mysql_pconnect('localhost');
mysql_select_db('test2');

mysql_query("insert into test values ('$name','$topic')");

?>


the fields are called name and topic.

the problem is now, that only the first field gets added data...is there something worong with the html file or the php file?
 
is there something worong with the html file or the php file?
I don't know yet, as I still don't have many details about your situation. Try as best you can to answer the following questions:

1) Are name and topic both string fields in your database (i.e. varchar, char, or text field types)? An ideal answer would be the SQL that defines the structure of the whole table (usually ascertained from Export in a DB maintenance tool like phpMyAdmin), but if you don't understand that, at least tell me the field types.

2) When you say, "Only the first field gets added data", is it the correct data, from what you typed in the "name" field on the form?

3) Did you try my suggested code? Put the following code in your PHP file, and then tell me what is shown on the screen and what you get in the database:
Code:
<?php
echo "Value of name is '".$_POST['name']."', and value of topic is '".$_POST['topic']."'<br>";
echo "Register_globals versions are '".$name."' and '".$topic."'<br>";
mysql_pconnect('localhost');
mysql_select_db('test2');
$sql = "insert into test(name,topic) values ('{$_POST['name']}','{$_POST['topic']}')";
if (!mysql_query($sql)) {
  echo("SQL Error ".mysql_errno().": ".mysql_error()." ($sql)");
  exit;
}
?>
At that point in the code I would also be tempted to query the database for the record you just added, but without knowing your database structure (what is the primary key of the table), I don't know how best to ask for it.

By the way, in the above code are several examples of good practice that you should learn from:
1) Always check the return value of mysql_query and exit out of your code with an error if it fails. Otherwise you are running blind, not knowing what difficulties your system is having (for example, unless you are doing more work with the field contents than you are telling us, the SQL statement will fail if someone fills in a name with an apostrophe, like O'Brien, or a topic like "I'm stuck").
2) When you have a problem, litter your code with statements that tell you what is going on at each step - in PHP, echo statements are handy; in Javascript, alerts are useful.
3) When inserting a new record, it is safest to always specify the fields, so that if the structure changes later (e.g. you decide to add an ID field at the beginning of the structure; if you did that, your insert statement would try to put the name in the ID field and the topic in the name field).
4) Always use the proper reference for $_POST (and $_GET) variables from forms, rather than depending on the PHP configuration "register_globals" to be on (which is what you are doing when you refer to them as simply $name and $topic). For security reasons, register_globals shouldn't be on anyway, but even if it is, you should use $_POST because then someone can't hack your code by typing "domain-path.../add_workin.php?name=Me&topic=MyEvilPlot" in their browser address bar, like they can the way your code is now.

I hope I haven't overwhelmed you with information, but as you are just getting started, I thought I would give you some useful pointers.
 
Try something like this:
Code:
// open the connection
$conn = mysql_connect("localhost", "", "");

// pick the database to use
mysql_select_db("test2",$conn);

//SQL Statement
$sql = "INSERT INTO test (namefield, topicfield) VALUES ('$_POST[name]', '$_POST[topic]')";


if (mysql_query($sql, $conn)) {
	print "Your item has been added";
					   
}else{
	echo "Something went wrong";
}

Reality is built on a foundation of dreams.
 
That's basically the same thing as I was suggesting. See, cnagra, I'm not the only one who thinks it's basic good practice to list the database fields in an insert statement and to explicitly reference your POST variables.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top