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

<form method=\"post\" action=\"insert.php\"> don,t wo

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I tried the following codes to test mysql in my linux server
1- to create a MySQL database :

<?php

$result = mysql_create_db (&quot;test_database&quot;);
if ($result == false)
echo mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<BR>&quot;;

?>

after running this code, the database was created successfully

2- to connect to a database and execute an SQL query. this code is to create a table that holds email information for a person:

<?php

$connection = mysql_connect (&quot;192.168.0.2:3306&quot;, &quot;root&quot;, &quot;mypassword&quot;);
if ($connection == false){
echo mysql_errno().&quot;: &quot;.mysql_error().&quot;<BR>&quot;;
exit;
}

$query = &quot;create table email_info &quot; .
&quot;(fullname varchar(255), email_address varchar(255))&quot;;
$result = mysql_db_query (&quot;test_database&quot;, $query);

if ($result)
echo &quot;Table 'email_info' was successfully created!&quot;;
else
echo mysql_errno().&quot;: &quot;.mysql_error().&quot;<BR>&quot;;

mysql_close ();


?>
after running the code, the connection and the tables was created successfully too.
3- When the following code is executed by a browser, it will show a form requesting a name and an email address. Once the submit button is clicked, the two fields will be stored into the table 'email_info', in the test_database.:
<?php

if ($submit == &quot;click&quot;){
// The submit button was clicked!
// Get the input for fullname and email then store it in the database.
$connection = mysql_connect (&quot;192.168.0.2:3306&quot;, &quot;root&quot;, &quot;mypasswrd&quot;);
if ($connection == false){
echo mysql_errno().&quot;: &quot;.mysql_error().&quot;<BR>&quot;;
exit;
}

$query = &quot;insert into email_info values ('$fullname', '$email')&quot;;
$result = mysql_db_query (&quot;test_database&quot;, $query);
if ($result){
echo &quot;Success!&quot;;
}
else{
echo mysql_errno().&quot;: &quot;.mysql_error().&quot;<BR>&quot;;
}

mysql_close ();
}
else{
echo &quot;
<html><body>

<form method=\&quot;post\&quot; action=\&quot;insert.php\&quot;>

Enter your full name
<input type=\&quot;text\&quot; name=\&quot;fullname\&quot;></input><br>
Enter your email address
<input type=\&quot;text\&quot; name=\&quot;email\&quot;></input><br>

<input type=\&quot;submit\&quot; name=\&quot;submit\&quot; value=\&quot;click\&quot;></input>

</form>

</body></html>
&quot;;
}

?>

but the two fields data never stored into the table, just refresh the page when the submit button is clicked. User “root” has full privileges to insert, delete, etc… I tried many codes to insert data into tables but always face this same problem !!
Thanks in advance for your help.
 
try this instead:

$query = &quot;insert into email_info (fullname,email_address) values ('$fullname', '$email')&quot;;
***************************************
Party on, dudes!
[cannon]
 
I tried it, but don't want to insert the data !!
 
missed this bit :
$result = mysql_db_query(&quot;test_database&quot;, $query);

Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.


mysql_select_db(&quot;test_database&quot;,$connection);
$result=mysql_query($query,$connection);
***************************************
Party on, dudes!
[cannon]
 
Thank you KarveR,
I tried it too but I didn’t get any error and the data didn’t insert into the table, I don't understand what happen. Can you write a simple code to insert a specific data (i.e. fullname: hisham and email_address: hisham@myserver.com) into table email_info without using form requesting a name and an email address, because I want to test if I can insert data into mysql database.
Thanks again.
 
$query=&quot;INSERT INTO email_info VALUES ('$fullname', '$email')&quot;;
mysql_query($query);

I think that'll work. divine_dragon9999@yahoo.com
 
OK heres a bit with some error checking to see if you are having errors on insert:
-----------------------------------------------------------
$connection = mysql_connect (&quot;192.168.0.2:3306&quot;, &quot;root&quot;, &quot;mypassword&quot;);
$db=&quot;test_database&quot;;
// here we check what errors if any are given
$err_no=mysql_errno();
if ($err_no > 0 ){

// if errors show them
echo mysql_errno() . &quot;: &quot; . mysql_error() . &quot;\n&quot;;

}
// here we select which database to use
@mysql_select_db($db,$connection) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot; Error: &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

// this is your query to be called
$sql=&quot;INSERT into email_info (fullname,email_address) values ('$fullname', '$email')&quot;;

// this will echo your query to the screen to check that the variables have a value
echo &quot;$sql<br>&quot;;

// connect to the db and run the insert
$insert=mysql_query($sql, $connection) ;

// here we check what errors if any are given
$err_no=mysql_errno();
if ($err_no > 0 ){

// if errors show them
echo mysql_errno() . &quot;: &quot; . mysql_error() . &quot;\n&quot;;

}

//show number of rows affected on successful query
printf (&quot;Records inserted: %d\n&quot;, mysql_affected_rows()); ***************************************
Party on, dudes!
[cannon]
 
Thank you ,
The code successfully inserted the data into the table, so I can be sure that the server and PHP and mysql work without errors. But I don’t understand why when I use form requesting a name and an email address, the code don’t insert the data and don’t give any error message, it only seems refresh the page!!
 
Looking at your code I cant see where you select the database to insert the code into.

@mysql_select_db($db,$connection) ; ***************************************
Party on, dudes!
[cannon]
 
I created an html page contains the following form:

<form action=&quot;insert.php&quot; method=&quot;post&quot;>
<input name=&quot;fullname&quot;>
<input name=&quot;email_address&quot;>
<input type=&quot;submit&quot; value=&quot;submit&quot;>
<input type=&quot;reset&quot; value=&quot;Clear&quot;>
</form>
the insert php:

<?php

$connection = mysql_connect (&quot;192.168.0.2:3306&quot;, &quot;root&quot;, &quot;lino&quot;);
$db=&quot;test_database&quot;;
// here we check what errors if any are given
$err_no=mysql_errno();
if ($err_no > 0 ){

// if errors show them
echo mysql_errno() . &quot;: &quot; . mysql_error() . &quot;\n&quot;;

}
// here we select which database to use
@mysql_select_db($db,$connection) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot; Error: &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

// this is your query to be called
$query = &quot;insert into email_info values ('$fullname', '$email')&quot;;


// connect to the db and run the insert
$insert=mysql_query($query, $connection) ;

// here we check what errors if any are given
$err_no=mysql_errno();
if ($err_no > 0 ){

// if errors show them
echo mysql_errno() . &quot;: &quot; . mysql_error() . &quot;\n&quot;;

}

//show number of rows affected on successful query
printf (&quot;Records inserted: %d\n&quot;, mysql_affected_rows());

?>

I get the following message: Records inserted: 1

But the records was blank, and not as I entered in the form.

I replaced
$query = &quot;insert into email_info values ('$fullname', '$email')&quot;;
With:
$sql=&quot;INSERT into email_info (fullname,email_address) values ('$fullname', '$email')&quot;;
And
$insert=mysql_query($query, $connection) ;
With
$insert=mysql_query($sql, $connection) ;

But I get the same result???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top