|
-
Sep 18th, 2003, 07:54 PM
#1
Thread Starter
Lively Member
poop (mysql) *RESOLVED*
PHP:
--------------------------------------------------------------------------------
<?php
$username = "morrowasted";
$password = "[edited]";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
$selected = mysql_select_db("www",$dbh)
or die("Could not select first_test");
if (mysql_query("insert into people values('4','$name1','$post1')")) {
print "successfully inserted record";
}
else {
print "Failed to insert record";
}
mysql_close($dbh);
mysql_close($dbh);
?>
--------------------------------------------------------------------------------
PHP:
--------------------------------------------------------------------------------
<?php
$username = "morrowasted";
$password = "[edited]";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("www",$dbh)
or die("Could not select first_test");
$result = mysql_query("SELECT id, name,post FROM people");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<div style='background:black;font:10pt;color:white'>";
echo "<center>";
print " Name:".$row{'name'}." <br>Comment:
".$row{'post'}."<br><br>";
echo "</center>";
print "</div>";
}
mysql_close($dbh);
?>
--------------------------------------------------------------------------------
PHP:
--------------------------------------------------------------------------------
<html>
<title> Post Comment </title>
</head>
<body bgcolor=black>
<form action="test.php" method="get">
Name: <input type="text" name="name1" value="Enter Username"><br>
Comment: <input type="text" name="post1" value="comment" size="20"><br>
<input type="submit" value="Post">
</form>
--------------------------------------------------------------------------------
everything works except im getting my "Failed to insert record" error (its not inserting it).
Last edited by morrowasted; Sep 18th, 2003 at 08:58 PM.

-morrowasted
-
Sep 18th, 2003, 07:58 PM
#2
Stuck in the 80s
Code:
mysql_query("insert into people values('4','$name1','$post1')")
I believe you need to specify field names? Such as:
Code:
mysql_query("INSERT INTO people (id, name, post) VALUES ('4','$name1','$post1')")
Not sure, though. The BEST way to find out what's going on is to do this:
Code:
$result = mysql_query("INSERT INTO people (id, name, post) VALUES ('4','$name1','$post1')")
or die(mysql_error());
if ($result) {
//do your cool stuff here...
} else {
//echo failed...
}
Then the error will tell you exactly what's not right.
-
Sep 18th, 2003, 08:07 PM
#3
Thread Starter
Lively Member

-morrowasted
-
Sep 18th, 2003, 08:25 PM
#4
Stuck in the 80s
Was I right about the field names, or was it something else?
-
Sep 18th, 2003, 08:59 PM
#5
Thread Starter
Lively Member
yeah, it was the field names.

-morrowasted
-
Sep 19th, 2003, 05:54 PM
#6
Frenzied Member
you don't need field names. but you must have all of the values for every field in that table. by default it will add to all, unless you specify fieldnames.
-
Sep 19th, 2003, 11:39 PM
#7
Thread Starter
Lively Member
Originally posted by phpman
you don't need field names. but you must have all of the values for every field in that table. by default it will add to all, unless you specify fieldnames.
well, it works, so thats all that matters right now

-morrowasted
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|