Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Inserting Form Data using PHP and MySQL?
Old 01-09-2009, 02:59 PM Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
Two things...I can't insert form data onto my 1and1 MySQL database, nor can I insert it onto my PC's MySQL database.

I've created all the necessary tables from the command line on the MySQL monitor, and on the web host's server.

First off, how do I access a table inside the database? I copied this code from a text book example and changed the mysql_pconnect() and mysql_select_db() to what I think is the correct info. When I go to the site and enter data in the html form, I get the php page prompt telling me "You have not entered all of your account information...." Am I not pointing my query down the right database connect path, or not even connecting to the web host? On the host server, there is a random dbname and a name for the table "books" that I need to access to insert the form data. How do I point the query to that table?

PHP Code:
<html>
<head>

<title>BOOK ENTRY RESULTS</title>
</head>

<body>
<h1>BOOK ENTRY RESULTS</h1>
<?
    
if (!$isbn || !$author || !$title || !$price)
{
    echo 
"You have not entered all of your account information.<br>"."Please fill out the form completely.";
    exit;
}

$isbn addslashes($isbn);
$author addslashes($author);
$title addslashes($title);
$price doubleval($price);


$db mysql_pconnect("db####.host.net""dbo#########""######");

if (!
$db)
{
    echo 
"Couldn't connect to database";
    exit;
}

mysql_select_db("db#########");
$query "insert into db######### values('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result mysql_query($query);
if (
$result)
    echo 
mysql_affected_rows()." book inserted into database.";

?>
Also, how would the code look if I wanted to connect to my "localhost" which seems easier. How do I know what my "localhost", user name and pw are to populate the MySQL database on my home PC?

Last edited by YOZ; 01-09-2009 at 03:11 PM..
YOZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-09-2009, 03:59 PM Re: Inserting Form Data using PHP and MySQL?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
About the validation:
The check in your script will only work if you have register_globals on, which basically means that any data passed to the script in GET or POST form will be set as an actual variable and not only in the GET and POST arrays.

This doesn't mean that you should switch register_globals to on, though, because it's not advised to do so regarding security.
What you want to do is change the $isbn, $author, $title and $price to $_GET['isbn'], $_GET['author] etc. when you're using a form with action="get". If you're using a form with action="post" you should change them to $_POST['isbn'], $_POST['author'] etc..


About the query:
I think you're trying to insert something in the database, right?
Seeing that 'into db#########' and 'mysql_select_db("db#########");' look the same.

You should be inserting data into a table and not into the database as a whole, so you should change 'into db#########' to 'into tablename'.
Then it should work.

And I'm also wondering why you're using a persistent connection. I'm not familiar with those so you might have a reason for it, but I think you should stick with mysql_connect for now instead.


About your own MySQL:
If you're running XAMPP or any other program of that sort, you can probably connect to your localhost with the following:
host: localhost
user: root
pass: '' (being an empty string)
If you haven't touched the root account, that is.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 04:31 PM Re: Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
QUESTIONS:

1. After changing the database name to the table name "books" I'm still having the same problems. The database name is db#########, so the function mysql_select_db() can have a table name inside, or do I need to call the database then select the table from the database using some sort of query syntax?

2. Do I need to install the latest PHP ver. on my PC to use my PC as the database server? The book I was using was using syntax from version 3.22 as it was published in 2001.

3. When conncecting to "localhost", is the user name and pw my login info for Vista? Or is the code actually:

PHP Code:
$db mysql_connect("localhost""root""pw####"); 
4. Regarding this part:

PHP Code:
mysql_select_db("books");
$query "insert into books values('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result mysql_query($query);
if (
$result)
    echo 
mysql_affected_rows()." book inserted into database."
are you saying to put _POST and or _GET infront of the row name? What are you refering to change with that comment?

Last edited by YOZ; 01-09-2009 at 04:50 PM..
YOZ is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 04:51 PM Re: Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
Here's my HTML form:

HTML Code:
<html>
<head>
    <title>NEW BOOK</title>
</head>

<body>
<h1>NEW BOOK</h1>

<form action='insert_book.php' method="post">
<table border="0">

   <tr>
     <td>ISBN</td>
      <td><input type="text" name="isbn" maxlength=13 size="13"><br></td>
   </tr>
   
   <tr>
     <td>Author</td>
     <td><input type="text" name="author" maxlength=30 size="30"><br></td>
   </tr>
   
   <tr>
     <td>Title</td>
     <td><input type="text" name="Title" maxlength=60 size="60"><br></td>
   </tr>
   
   <tr>
     <td>Price $</td>
     <td><input type="text" name="price" maxlength=7 size="7"><br></td>
   </tr>
   
   <tr>
           <td colspan="2"><input type="submit" value="Register"></td>
   </tr>
   </table>
   </form>
   </body>
   </html>
YOZ is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 05:59 PM Re: Inserting Form Data using PHP and MySQL?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
As you can see in your form you have method="post", so you should call $_POST['isbn'] in PHP instead of $isbn.
This is because of the register_globals being off as I posted before.
In older versions of PHP it was very common to code that way until people began to see that it was highly insecure, so now register_globals is mostly switched off.

For the function mysql_select_db() you should use the database name db######
and for the query you should use the table name: insert into tablename.

PHP Code:
<body>
<h1>BOOK ENTRY RESULTS</h1>
<?
    
if (empty($_POST['isbn'] || empty($_POST['author'] || empty($_POST['title'] || empty($_POST['price']))
{
    echo 
"You have not entered all of your account information.<br>"."Please fill out the form completely.";
    exit;
}

$isbn addslashes($_POST['isbn']);
$author addslashes($_POST['author']);
$title addslashes($_POST['title']);
$price doubleval($_POST['price']);


$db mysql_connect("db####.host.net""dbo#########""######");

if (!
$db)
{
    echo 
"Couldn't connect to database";
    exit;
}

mysql_select_db("db#########");
$query "insert into db######### values('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result mysql_query($query);
if (
$result)
    echo 
mysql_affected_rows()." book inserted into database.";

?>

Please try this and tell me if it works, and where it fails otherwise.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 07:00 PM Re: Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
Parse error: syntax error, unexpected T_BOOLEAN_OR, expecting ')' in /homepages/7/d211025025/htdocs/home/insert_book.php on line 10

Referring to
PHP Code:
if (empty($_POST['isbn'] || empty($_POST['author'] || empty($_POST['title'] || empty($_POST['price'])) 

Also, I downloaded XAMPP and ended up having to uninstall it. I even uninstalled MySQL 5.1 that I had installed in order to free up all ports. Now every time I try to reinstall it, when I try to start the MySQL admin, it gives me repeating error window until I disable the tool.

Last edited by YOZ; 01-09-2009 at 07:12 PM..
YOZ is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 07:04 PM Re: Inserting Form Data using PHP and MySQL?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Oh my bad:
PHP Code:
if (empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['price'])) 
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 07:23 PM Re: Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
BOOK ENTRY RESULTS

You have not entered all of your account information.
Please fill out the form completely.


Something is still going on...however it is showing the php page again! I have the login info right...and the right dbname and tablename...

Does this mean I'm connected since it didn't give the "Couldn't connect to database" prompt? Or is the code not even reaching the mysql_select_db() function?

Here is the current code:

PHP Code:
<html>
<head>

<title>BOOK ENTRY RESULTS</title>
</head>

<body>
<h1>BOOK ENTRY RESULTS</h1>
<?
    
if (empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['price']))  

{
    echo 
"You have not entered all of your account information.<br>"."Please fill out the form completely.";
    exit;
}

$isbn addslashes($_POST['isbn']);
$author addslashes($_POST['author']);
$title addslashes($_POST['title']);
$price doubleval($_POST['price']);


$db mysql_connect("db####.HOST.net""dbo#########""#######");

if (!
$db)
{
    echo 
"Couldn't connect to database";
    exit;
}

mysql_select_db("db#########");
$query "insert into books values('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result mysql_query($query);
if (
$result)
    echo 
mysql_affected_rows()." book inserted into database.";

?>


<br>
</body>
</html>

Last edited by YOZ; 01-09-2009 at 07:31 PM..
YOZ is offline
Reply With Quote
View Public Profile
 
Old 01-09-2009, 08:33 PM Re: Inserting Form Data using PHP and MySQL?
Defies a Status

Posts: 1,605
Trades: 0
Yoz.

You have several issues going on here. I copied the files to look at.


Almost all of the advice you were given is correct but you kept moving the files from localhost to the half&half service.



I will post back later.

EDIT: I have to ask if you got a sql dump with these scripts or instructions on how to create the tables within the database?

I have it working but I don't have a table to write the data to.
__________________
Colbyt

Please login or register to view this content. Registration is FREE

Last edited by colbyt; 01-09-2009 at 09:42 PM.. Reason: EDIT:
colbyt is offline
Reply With Quote
View Public Profile
 
Old 01-12-2009, 09:53 PM Re: Inserting Form Data using PHP and MySQL?
YOZ
Novice Talker

Posts: 14
Name: Matt Yoswa
Trades: 0
Thanks colbyt for helping me with this issue!

The zip file you sent me worked great after I changed the link from the HTML form to the PHP form page. I now have my first entry into my database!

Really over and above what I was looking for. Not only did colbyt help resolve the issue, he included other simple PHP codes for future use, and answered questions I hadn't even thought of yet!!
YOZ is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Inserting Form Data using PHP and MySQL?
 

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44986 seconds with 12 queries