LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-15-2005, 07:22 PM   #1
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 13
Posts: 288

Rep: Reputation: 32
mysql and php issue


I am trying to make a php form on the web that will write data to and query data from an existant database. When I attemp to do this, I get the return info

query($query); if ($result) echo 'Record entered.'; $db->close(); ?>

regardless of whether or not I actually entered any data into the form to begin with. As I understand it, it should be stopping the script at the first if statement when none of the variables have any values. Here is the html form for the input, followed by the php script that should be connecting to my database. I know the php form isn't complete with data input, I removed most of the queries to simplify this. Thanks in advance for any help.

<!--This page is the main php file for the MySQL interface for the UTHookup Database-->

<HTML>
<HEAD><TITLE>
Database
</TITLE></HEAD>
<BODY>

<H2><B>Registration Info</B></H2>


<FORM ACTION="processinfo.php" METHOD="post">

<TABLE>
<TR>
<TD WIDTH=100>First Name:</TD>
<TD><INPUT TYPE="text" NAME="firstname" MAXLENGTH=50>
Last Name: <INPUT TYPE="text" NAME="lastname" MAXLENGTH=50></TD>
</TR>
<TR>
<TD>Current Address:</TD>
<TD><INPUT TYPE="text" NAME="streetaddress" SIZE =50 MAXLENGTH=255></TD>
</TR>
<TR>
<TD>City:</TD>
<TD><INPUT TYPE="text" NAME="city" MAXLENGTH=50 VALUE="Austin"> State: <INPUT TYPE="text" NAME="state" SIZE=2 MAXLENGTH=2 VALUE="TX">
Zip: <INPUT TYPE="text" NAME="zipcode" SIZE=5 MAXLENGTH=5></TD>
</TR>
<TR>
<TD>Telephone:</TD>
<TD><INPUT TYPE="text" NAME="areacode" SIZE=3 MAXLENGTH=3> <INPUT TYPE="text" NAME="telephonenumber" SIZE=7 MAXLENGTH=7>
E-Mail: <INPUT TYPE="text" NAME="email" SIZE=30 MAXLENGTH=50></TD>
</TR>
<TR>
<TD>Gender:</TD>
<TD><INPUT TYPE=RADIO NAME="gender" VALUE=Male>Male<BR>
<INPUT TYPE=RADIO NAME="gender" VALUE=Female>Female<BR>
</TD>
</TR>
<TR>
<TD COLSPAN=2><HR></TD>

<TR>
<TD>Hometown Address</TD>
<TD><INPUT TYPE="text" NAME="hometownaddress" SIZE =50 MAXLENGTH=255></TD>
</TR>
<TR>
<TD>City:</TD>
<TD><INPUT TYPE="text" NAME="hometowncity" MAXLENGTH=50> State: <INPUT TYPE="text" NAME="hometownstate" SIZE=2 MAXLENGTH=2>
Zip: <INPUT TYPE="text" NAME="hometownzipcode" SIZE=5 MAXLENGTH=5></TD>
</TR>
<TR>

<TD COLSPAN=2><HR></TD>
</TR>

<TR>
<TD>Camp:</TD>
<TD><SELECT NAME="campname">
<OPTION VALUE=1>Donkey Kong
<OPTION VALUE=2>Luigi
<OPTION VALUE=3>Mario
<OPTION VALUE=4>Princess
<OPTION VALUE=5>Yoshi
</SELECT>
</TD>
</TR>
<TR>
<TD COLSPAN=2><HR></TD>
</TR>
<TR>
<TD>Notes:</TD>
<TD>
<TEXTAREA ROWS=4 COLS=50 NAME="note" WRAP=VIRTUAL>
</TEXTAREA>
</TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Submit Camper Information">
<INPUT TYPE="reset" VALUE="Reset Form"></TD>
</TR>
</TABLE>

</FORM>


</BODY>
</HTML>
and the php form is



<HTML>
<HEAD>
<TITLE>Data Entered</TITLE>
</HEAD>
<BODY>

<?php
// create short variable names
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$areacode = $_POST['areacode'];
$telephonenumber = $_POST['telephonenumber'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$hometownaddress = $_POST['hometownaddress'];
$hometowncity = $_POST['hometowncity'];
$hometownstate = $_POST['hometownstate'];
$hometownzipcode = $_POST['hometownzipcode'];
$campname = $_POST['campname'];
$note = $_POST['note'];

if (!$firstname || !$lastname || !$streetaddress || !$city || !$state || !$zipcode || !$gender || !$campname)
{
echo 'You have not entered all required information,'
echo 'Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$firstname = addslashes($firstname);
$lastname = addslashes($lastname);
$streetaddress = addslashes($streetaddress);
$city = addslashes($city);
$areacode = addslashes($areacode);
$telephonenumber = addslashes($telephonenumber);
$email = addslashes($email);
$hometownaddress = addslashes($hometownaddress);
$hometowncity = addslashes($hometowncity);
$note = addslashes($note);
}

@ $db = new mysqli('localhost', 'username', 'password', 'database');

if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please contact database admin.';
exit;
}

$query = "INSERT INTO camper VALUES
('', '".$campname"', '".$firstname"', '".$lastname"', '".$gender"')";

$result = $db->query($query);
if ($result)

echo 'Record entered.';
$db->close();
?>
</BODY>
</HTML>


I have of course changed some of the actual info (the mysqli command, for example), but beside that, this is what I have. I have double checked and triple checked to make sure the database is active and listening, that the user I have is able to insert data, and that my fields are all correct. I suspect that the variables aren't getting any values at all to begin with, though, since even if i submit the form with all blanks, it is not stopped at the first if statement. The error I'm getting isn't really even an error, just a line from the script

query($query); if ($result) echo 'Record entered.'; $db->close(); ?>

but the database isn't being connected to. I do have the latest version of php installed, and I am hosting several php pages from my webserver right now that work just fine.

Last edited by czarherr; 03-15-2005 at 08:06 PM.
 
Old 03-15-2005, 09:43 PM   #2
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 13
Posts: 288

Original Poster
Rep: Reputation: 32
ok, I've tried a few things, here is the case now. I put the code up on my webserver and surfed to it. If i leave the values NULL, i get the error that I need to enter my data again, just like i wanted. The only problem now is that if i enter all the data, i get a blank screen, and the database is untouched. No errors, nor does it show me any of the error messages i put in the if statement in case it couldnt connect. this line appears often in my /var/log/apache/error_log

[error] PHP Notice: Undefined index: section in /var/www/htdocs/index.php on line 2

as well as

[error] PHP Notice: Undefined index: gender in /var
/www/htdocs/processinfo.php on line 12

any clues?
 
Old 03-16-2005, 01:44 AM   #3
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 13
Posts: 288

Original Poster
Rep: Reputation: 32
nm, figured it out
 
Old 04-07-2005, 08:39 AM   #4
flashingcurser
Member
 
Registered: Jan 2003
Distribution: many win/nix/mac
Posts: 259

Rep: Reputation: 31
Can you remember what did you do to fix this? I'm doing almost the same thing and getting similar results.

Thanks
dan
 
Old 04-07-2005, 05:36 PM   #5
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 13
Posts: 288

Original Poster
Rep: Reputation: 32
I was using the wrong version of mysql. The code I used uses mysqli() for the connections, but I was using an old version of PHP3 and an old version of MySQL. Upgrade them both and you'll be fine. If you do have the latest version of PHP and MySQL, make sure that you used the --with-mysqli switch in your ./configure.
 
  


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
Php mysql issue petenyce Linux - Software 4 09-23-2005 02:39 AM
Problem getting PHP to recognize MySQL, Using PHP 4.0 and MySQL 4.0.20 d2army Programming 4 06-27-2004 08:54 PM
php4 mysql, installation, php-pages with mysql info stay empty dnla Linux - Software 2 03-14-2004 02:54 PM
PHP + MySQL Integration issue digitalgravy Linux - Software 1 01-15-2004 10:07 AM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM


All times are GMT -5. The time now is 06:59 AM.

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