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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP submits data to MySQL but database not updating 1

Status
Not open for further replies.

Seilsong

Programmer
May 3, 2002
11
0
0
GB
Ok, heres the problem.
A message submit form in PHP takes some parameters from user entered data and runs two MySQL queries to get, specifically a user_id value based on the username entered,and a forum_id value based on the forum name entered.These are made $variables and added to an INSERT query.
When the form is submitted, a successful operation is reported, yet the two values retrieved by the queries are not entered into the database.
The query and variable syntax are spot on. No reported parse errors or SQL errors.
My data type specs in the database for the two id values are 'mediumint'. Does a PHP $ variable need to be of the correct data type for it to be accepted by the database?? should I change the required data type in the database to VARCHAR ??..All other values retrieved from the form insert ok into the database......any help would be appreciated.
 
What you should really do is post the code so we can look over it. Your description sounds fine, yet we cannot verify that you are not basing it on false assumptions.
sleipnir214 would write: Not enough data for meaningful answer. ;)
 
Yo DRJ478
Thanks for such quick response
A lot of code for a message board but here goes.


<?php #Script 1.0.1 - addmessage.php

if(isset($_POST['submit'])){ // Handle the form
$message = NULL; // create an empty new variable

// check for a username
if(empty($_POST['username'])){
$un = FALSE;
$message .= '<p> You did not enter your user name</p>';
}else{ $un = $_POST['username'];
}
// check for a message title
if(empty($_POST['title'])){
$ti = FALSE;
$message .= '<p> You did not enter a message title!</p>';
}else{ $ti = $_POST['title'];
}

// check for a forum
if(empty($_POST['forum_name'])){
$fn = FALSE;
$message .= '<p> You did not enter a Forum Name!</p>';
}else{ $fn = $_POST['forum_name'];
}

// check for a message
if(empty($_POST['message'])){
$mg = FALSE;
$message .= '<p> You did not enter a message!</p>';
}else{ $mg = $_POST['message'];
}


if($un && $ti && $fn && $mg){ // if all fields completed
// submit the message to the database

require_once('mysql_connect.php'); //connect to the database

// Get the user id from username username supplied in form
$query1 = "SELECT user_id FROM users WHERE username=$un ";
$uid = @mysql_query($query1);

// Get the forum id from forum name supplied in form
$query2 = "SELECT forum_id FROM forums WHERE forum_name=$fn ";
$fid = @mysql_query($query2);

$query = "INSERT INTO messages ( message_id, forum_id, title, message, date, time, user_id)
VALUES( NULL,'$fid', '$ti', '$mg', CURDATE(), CURTIME(), '$uid')";
// run the query
$result = @mysql_query($query);
if($result){ // if all ran ok
echo '<p><b> Thank you. Your message has been submitted. </b></p>';
exit(); // close the script
}else{ // if problems running the script
$message .= '<p> Your message could not be submitted due to a system error </p><p>' .mysql_error(). '</p>';
}

mysql_close(); //close the database connection

}else{ $message .= '<p> Please try again.</p>';
 
All right, this actually is pretty easy.
mysql_query() returns a resource id, not the actual record set. You need to use mysql_fetch_assoc() or a similar function to get the actual record from the resource.
Once you do that you'll have all settled.
I wrote a FAQ on that faq434-3850 - it will explain all steps (you've connected ok, so read the later part).
Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top