how to connect to mysql and insert data

habib

New member
Joined
May 29, 2013
Messages
8
hello,
help me
I have a page that connect to database in my computer and insert data in table but in server Cannot connect database
and I have message end of page .
"; echo "successful"; echo "
"; echo "back"; } else { echo "error"; } ?>

I do not how to solve the above problems?
Thanks & Best regards.
:confused:
 
connection is mistake

Thanks
But just one Access in Host "localhost "and
I want insert data in page and add to table in mysql bank
But no connect
Or connection is mistake
Thanks & Best regards
:confused:
 
Sorry, I don't understand what issue you facing there. Please post a screenshot with an error or full text of an error, you get there...

Just a guess, if your PHP script is located on different server, and MySQL DB is locate on another one, then you might need to check firewall as well.
 
my code

I have two page
1-insert.php:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="htmlform" method="get" action="insert_ac.php" dir="rtl">
<table width="450px">
<td width="153"></tr>
<tr>
 <td valign="top">
  <label for="name">name*</label>
 </td>
 <td width="285" valign="top"><input  type="text" name="name" maxlength="50" size="30" /></td>
</tr> 
<tr>
 <td valign="top">
  <label for="codv">code*</label>
 </td>
 <td valign="top">
  <input  type="text" name="codv" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="codm">code2*</label>
 </td>
 <td valign="top">
  <input  type="text" name="codm" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="mail">mail </label>
 </td>
 <td valign="top">
  <input  type="text" name="mail" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="tel">phone</label>
 </td>
 <td valign="top">
  <input  type="text" name="tel" maxlength="30" size="30">
 </td>
</tr>
<tr>
<td valign="top">
  <label for="mozdar"> Topic </label>
 </td>
 <td valign="top">
  <input  type="text" name="mozdar" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="dar"> Request *</label>
 </td>
 <td valign="top"><textarea  name="dar" cols="27" rows="6"></textarea></td>
</tr>
<tr>
 <td colspan="2" style="text-align:center"><input type="submit" value="send" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="htmlform" />
</form>
</body>
</html>
2-insert_ac.php:
PHP:
<?php
$host="localhost";
$username="my user name bank;
$password="my pass bank"; 
$db_name="my db name";
$tbl_name="table name";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
// Get values from form
$name=$_GET['name'];
$codv=$_GET['codv'];
$codm=$_GET['codm'];
$mail=$_GET['mail'];
$tel=$_GET['tel'];
$mozdar=$_GET['mozdar'];
$dar=$_GET['dar'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, codv, codm, mail, tel, mozdar, dar)VALUES('$name', '$codv', '$codm','$mail','$tel','$mozdar','$dar')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "successful";
echo "<BR>";
echo "<a href='insert.php'>back</a>";
}
else {
echo "error";
}
?>
<?php
// close connection
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
That no inserts data in my bank.
Thanks & Best regards
:(
 
Last edited by a moderator:
I use mysql_error() and I observed all text error
i don't understand
:(
 
error

Hello
It is my error that all text in page:

HTML:
query("SELECT 1;")->fetch_assoc(); // Fatal error: Call to a member function fetch_assoc() on a non-object // Get values from form $name=$_GET['name']; $codv=$_GET['codv']; $codm=$_GET['codm']; $mail=$_GET['mail']; $tel=$_GET['tel']; $mozdar=$_GET['mozdar']; $dar=$_GET['dar']; // Insert data into mysql $sql="INSERT INTO $tbl_name(name, codv, codm, mail, tel, mozdar , dar)VALUES('$name', '$codv', '$codm','$mail','$tel','$mozdar','$dar')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){
echo "successful";
echo "<BR>";
echo "<a href='insert.php'>back</a>";

Is it possible to make mistake in installation proses of Mysql or Apache?:confused:

Thanks & Best regards
 
Is it possible to make mistake in installation proses of Mysql or Apache?

If you use stable version of custombuild script, without any customization, then I'd rather say No, it's not possible in 99% of cases...
And your error is hardly related to how MySQL or Apache is installed. It's either an error in your PHP script (I did not check how valid it is), or you are missing an obvious related to MySQL DB.

Sorry to say that, but these forums are not the right place to learn how to do PHP-coding. Still if you need a hand, I'm ready to assist you, and it will be a pleasure for me.

p.s. Probably you are missing mysql_select_db() in your PHP script.
 
but it is correctly in my computer ( in WampServer)and insert record.
:(
 
I do know nothing about either your PC or WampServer, since they are Windows related.

Did you add in your mysql_select_db() into your PHP script?
 
Back
Top