LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-03-2003, 06:22 PM   #1
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Rep: Reputation: 15
Problem Inserting MySQL data from PHP!


hi

i am using devshed tutorials to learn PHP and MySQL, and in one of the tests i succesed in getting data from MySQL, but failed to insert data there, Could some one look at the scripts and tell me what the problem is please.


add.php
----------------------------------------------
<html>
<head><title>Web Database Sample Inserting</title>
</head>

<body bgcolor=#ffffff>
<?
if (isset($name) && isset($phone)) {
mysql_connect("localhost", "webuser", "");
$query = "INSERT INTO mytable VALUES ('$name', '$phone')";
$result = mysql_db_query("example", $query);

if ($result) {
echo "<p>$name was added to the database</p>";
}
}
?>

<h1>Add an entry</h1>
<form>
Name: <input type=text name='name'><br>
Phone: <input type=text name='phone'><br>
<input type=submit>
</form>

<p><a href="index.php">Back to index</a>
</body>
</html>

index.php
----------------------------------------------
<html>
<head><title>Web Database Sample Index</title>
</head>

<body bgcolor=#ffffff>
<h1>Data from mytable</h1>
<?
mysql_connect("localhost", "webuser", "");
$query = "SELECT name, phone FROM mytable";
$result = mysql_db_query("example", $query);

if ($result) {
echo "Found these entries in the database:<ul>";
while ($r = mysql_fetch_array($result)) {
$name = $r["name"];
$phone = $r["phone"];
echo "<li>$name, $phone";
}
echo "</ul>";
} else {
echo "No data.";
}
mysql_free_result($result);
?>

<p><a href="add.php">Add new entry</a>
</body>
</html>
 
Old 03-03-2003, 06:27 PM   #2
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Does the user "webuser" have permission to INSERT into the database?
 
Old 03-03-2003, 06:31 PM   #3
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
thanks, yes, this is what i did

GRANT usage
ON example.*
To webuser@localhost

GRANT select, insert, delete
ON example.*
TO webuser@localhost;
 
Old 03-03-2003, 06:36 PM   #4
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
the insert statement looks wrong.
Shouldnt it be something like:

INSERT into TABLE (FIELD1,FEILD2) VALUES ('$feild1', '$field2');

How the hell do you spell feild anyway?
 
Old 03-03-2003, 06:37 PM   #5
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
field.

but i think from my reading this is not the problem.
 
Old 03-03-2003, 06:47 PM   #6
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Quote:
but i think from my reading this is not the problem.

Really?
How does this work then:

Quote:
$query = "INSERT INTO mytable VALUES ('$name', '$phone')";

mysql is telepathic and can guess which fields you want to insert the values of
$name and $phone into?

Mysql has indeed made great strides
 
Old 03-03-2003, 07:05 PM   #7
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
i will try it out
 
Old 03-03-2003, 07:06 PM   #8
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
I'm getting your sarcasim m0rl0ck but I agree. You must pass a string into the variables $name and $phone. Your current syntax does not to that.
 
Old 03-03-2003, 07:15 PM   #9
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
sorry if you though i was sarcastic, i am not, just tryin to answer minimly!!!
ok i changed it, but still get the same results.

<?
if (isset($name) && isset($phone)) {
mysql_connect("localhost", "webuser", "");
$query = "INSERT INTO mytable (name, phone) VALUES ('$name', '$phone')";
$result = mysql_db_query("example", $query);

if ($result) {
echo "<p>$name was added to the database</p>";
}
}
?>
 
Old 03-03-2003, 07:56 PM   #10
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Quote:
sorry if you though i was sarcastic, i am not,
Actually I think I was the one being accused of sarcasm and correctly so, mea culpa

It all looks ok to me though assuming the db "example" exists.

Try putting in:

echo $query;
echo "\n $name";
echo "\n $phone";

Right below the query to see whats being passed to the database and what the values of $name and $phone are being set to.




EDIT:
Hey you might want to look at this:
http://www.php.net/manual/en/functio...l-db-query.php

Especially:
"Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead. "

Last edited by m0rl0ck; 03-03-2003 at 08:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem inserting data into a mysql table using PHP Rockgod2099 Programming 13 08-03-2005 12:27 AM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
I need help with inserting data into mysql w/ PHP lostboy Programming 3 08-25-2003 12:08 PM
inserting data in mysql zuessh Linux - Software 4 05-19-2003 01:00 PM
MySQL - Inserting Data Not Happening! Rhapsodic Programming 4 10-26-2002 10:58 PM


All times are GMT -5. The time now is 10:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration