$result = mysql_query ($query);

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

$result = mysql_query ($query);

Post by comtek »

Below is a snip that I am playing with... everything appears to be working except:

$result = mysql_query ($query);

I get results from the $_POST and $query usig the print_r but nothing for the $results

Am I coding something wrong?



Code: Select all

if (isset($_POSTї'submit'])) {

require_once ('dbconnect.php'); // Connect to the database.

$query = "INSERT INTO patients (firstname, initial, lastname, dob, ) VALUES ('$firstname', '$initial', '$lastname', '$dob', NOW())";
	$result = mysql_query ($query);
	
echo '<pre>';
print_r ($result);
echo '</pre>';

echo '<pre>';
print_r ($query);
echo '</pre>';

echo '<pre>';
print_r ($_POST);
echo '</pre>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have an error in your SQL query... you did not name the field for which NOW() should be inserted into.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

a good way to check whats wrong is to change the $result line to:

$result = mysql_query($query) or die("Could Not Execute Query: ".mysql_error());
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

Post by comtek »

OK I changed the NOW() . Also part of my problem was in the dbconnect file... I had a mysql_close command at the end of the file.

Now i get the following message on my page:

DB SelectedColumn count doesn't match value count at row 1
I am not sure what this means.

Also, I have more $_POST array values that are not in the INSERT command... would that cause any problems?

Here is the script as it is now:

Code: Select all

<?php

include ('includes/header.html');

echo '<pre>';
print_r ($_POST);
echo '</pre>';

if (isset($_POST&#1111;'submit'])) &#123;

require_once ('dbconnect.php'); // Connect to the database.

$query = "INSERT INTO patients (firstname, initial, lastname, dob) VALUES ('$firstname', '$initial', '$lastname', '$dob', date=NOW())";
	$result = mysql_query ($query) or die(mysql_error()); 
	
echo '<pre>';
print_r ($result);
echo '</pre>';

echo '<pre>';
print_r ($query);
echo '</pre>';

&#125;

if (isset($firstname)) &#123; 
print "$firstname ";
&#125;
if (isset($initial)) &#123; 
print "$initial ";
&#125;
if (isset($lastname)) &#123; 
print "$lastname <br>";
&#125;
if (isset($dob)) &#123; 
print "$dob ";
&#125;
if (isset($social)) &#123; 
print "$social ";
&#125;
if ($address2 < null) &#123; 
print "Address: $address2 ";
&#125;










include ('includes/footer.html');

?>
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

Post by comtek »

seems to be comming from here:

Code: Select all

$select = mysql_select_db(DB_NAME);
if (!$select) &#123;
	die ('Could not select the database: ' . mysql_error());
&#125;
echo 'DB Selected';
Above is part of my dbconnect.php
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

Post by comtek »

Below is what I get on my screen:

Array
(
[MAX_FILE_SIZE] => 2524288
[firstname] => tommy
[initial] => w
[lastname] => bbaa
[dob] => 11-11-1
[address1] => 21212 ain
[address2] =>
[city] => jdoiaisd
[state] => dsa
[zip] => oskdoaska
[social] => 12121
[phone] => 121212
[submit] => Submit
)
Connected successfully


DB SelectedColumn count doesn't match value count at row 1
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

Post by comtek »

Sorry... Its late and I am stupid.

I was reading the message wrong.

My problem was that did not complete everything that the NOW needed.

It seems to be working now.
comtek
Forum Commoner
Posts: 39
Joined: Tue Feb 17, 2004 10:46 pm

Post by comtek »

Everything is working now, thanks to your help. Just one question though.

This bit of code:

Code: Select all

$query = "INSERT INTO patients (patient_num, gender, status, firstname, initial, lastname, dob, ss, address1, address2, city, state, zip, phone, employer, employer_addy, health_ins, pharm_cov, hourly_wage, weekly_hours, annual_salary, other_income, date) VALUES ( '$patient_num', '$gender', '$status', '$firstname', '$initial', '$lastname', '$dob', '$ss', '$address1', '$address2', '$city', '$state', '$zip', '$phone', '$employer', '$employer_addy', '$health_ins', '$pharm_cov', '$hourly_wage', '$weekly_hours', '$annual_salary', '$other_income',  NOW())";
	$result = mysql_query ($query) or die(mysql_error()); 
	
echo '<pre>';
echo "This is the result : <br>" ;
print_r ( $result);
echo '</pre>';

echo '<pre>';
echo "This is the query : <br>" ;
print_r ( $query);
echo '</pre>';

&#125;

if (isset($firstname)) &#123; 
print "$firstname ";
&#125;
if (isset($initial)) &#123; 
print "$initial ";
&#125;
if (isset($lastname)) &#123; 
print "$lastname <br>";
&#125;
if (isset($dob)) &#123; 
print "$dob ";
&#125;
if (isset($social)) &#123; 
print "$social ";
&#125;
if ($address2 < null) &#123; 
print "Address: $address2 ";
&#125;
Returns the following:

===============================================

...
[hourly_wage] => 4 carrots
[weekly_hours] => 40
[annual_salary] => 25000
[other_income] => 123
[submit] => Continue
)
Connected successfully


DB Selected

This is the result :
1

This is the query : INSERT INTO patients (patient_num.....

Bugs I Like Carrots Bunny
1922-12-3

============================================


The part that says "This result:

1


Is that coming from the "or die(mysql_error());" part of $result


Just curious as to what it means.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's from the print_r($result)

insert queries return true/false.
Post Reply