-2

我创建了我的 html 页面和我的 php 脚本来将表单数据推送到 mySQL 数据库,但是当我单击提交时,我得到了这个错误。

警告:mysql_connect() [function.mysql-connect]:在“读取初始通信数据包”时丢失与 MySQL 服务器的连接,系统错误:111 in

这是我的脚本:

<?php

$host="custsql-ipg06.eigbox.net"; // Host name 
$username="brawiz"; // Mysql username 
$password="mffirm2013"; // Mysql password 
$db_name="brawizard"; // Database name 
$tbl_name="orders_mysql"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$name=$_POST["name"];
$lastname=$_POST["lastname"];
$address=$_POST["address"];
$address2=$_POST["address2"];
$city=$_POST["city"];
$state=$_POST["state"];
$zip=$_POST["zip"];
$country=$_POST["country"];
$phone=$_POST["phone"];
$bra_size=$_POST["bra_size"];
$bra_color=$_POST["bra_color"];
$cami_size=$_POST["cami_size"];
$cami_color=$_POST["cami_color"];
$email=$_POST["email"];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(name, lastname, address, address2, city, state, zip, country, phone, bra_size, bra_color, cami_size, cami_color, email)VALUES('$name', '$lastname', '$address' '$address2', '$city', '$state', '$zip', '$country', '$phone', '$bra_size', '$bra_color', '$cami_size', '$cami_color', '$email')";
$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 to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>

<div class="seven columns">
        <center>
        First name:* <input type="text" name="name" required>
        Address 1:* <input type="text" name="address" required>
        City:* <input type="text" name="city" required>
        Zip Code:* <input type="text" name="zip" required>
        Email Address:* <input type="text" name="email" required>
        </center>
        </div>


        <div class="six columns">
        <center>
        Last name:* <input type="text" name="lastname" required>
        Address 2:* <input type="text" name="address2" required>
        State:* <input type="text" name="state" required>
        Country:* <input type="text" name="country" required>
        Phone Number:* <input type="text" name="phone" required>
        </center>
        </div>


        </div>
        </center>
        <div class="sixteen columns">
        <button type="submit" value="submit" name="Submit" style="margin-left:38%;"><img src="/images/order.jpg" alt="order"></button>  
        </form>
4

2 回答 2

2

这可能是一个问题,因为您的 MySQL 只允许本地 IP 连接,并且您可能正在从另一个 IP 地址运行您的脚本?

如果这不是问题,则可能是防火墙问题: http: //forums.mysql.com/read.php?52,166244,258515#msg-258515

于 2013-11-11T22:15:44.543 回答
0

第 11 行有一个错误。而不是执行
mysql_connect(),为什么不执行 $conn=mysql_connect() 并在第 12 行输入“​​$db_name”后输入逗号并输入“$conn”

例如:-

      $conn=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name",$conn)or die("cannot select DB");
于 2013-11-12T03:21:47.813 回答