错误:未选择数据库和未定义索引

时间:2017-03-02 06:32:20

标签: php

我被困在这里差不多一天我找不到合适的解决方案请帮助我收到错误,没有选择数据库但是我收到的消息是连接已创建 我得到的第二个错误是未定义的索引“SNo”“clientname”“contact no”“option-3(它的复选框)”

Html表单

<form  action="survey-data-entry.php" name="1st-form" id="form-control" onSubmit="return validate()" method="post" >    
    <label>S.No.</label>
<input type="text" class="form-control" placeholder=" 0451211315" name="SNo" id="s.no"  >
<label>Client Name</label>
<input type="text" class="form-control" name="clientname" id="clientname" />                
<label>Contact No</label>
<input type="text" class="form-control" name="contactno" id="contactno" />
    <strong>Pest Buster  </strong>
<input type="checkbox" name="option_3" id="option_3" value="Pest Buster">

这是PHP代码

$hostname = 'localhost';
$username ='root';
$password = '';
$dbname = 'survey-form';

$conn = mysql_connect($hostname, $username, $password, $dbname);
if(!$conn){
    die("connection failed:" .mysql_error());
}
echo "connection hogya successful";



$SNo = $_POST['SNo'];
     $clientname = $_POST['clientname'];
     $contactno = $_POST['contactno'];
     $option_3 = $_POST['option_3'];
$sql= "INSERT INTO php-startup (SNo, clientname, contactno, option_3)
VALUES('SNo', 'clientname', 'contactno ','option_3')";

if(!mysql_query($sql))
{
die('caution' . mysql_error());
}
echo"1 record added";

3 个答案:

答案 0 :(得分:1)

数据库连接

$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');

然后选择db

mysql_select_db('database_name',$connection);

然后执行查询

if(isset($_POST['SNo'])){
         $SNo = $_POST['SNo'];
         $clientname = $_POST['clientname'];
         $contactno = $_POST['contactno'];
         $option_3 = $_POST['option_3'];
    $sql= "INSERT INTO php-startup (SNo, clientname, contactno, option_3)
    VALUES('$SNo', '$clientname', '$contactno','$option_3')";

    if(!mysql_query($sql))
    {
        die('caution' . mysql_error());
    }
        echo"1 record added";
}

答案 1 :(得分:1)

我不知道&#34;没有选择数据库&#34;因为我没有使用mysql_*

的经验

您可以先检查变量是否已设置,如此

if(isset($_POST['SNo']))
{
    $SNo = $_POST['SNo'];
    $clientname = $_POST['clientname'];
    $contactno = $_POST['contactno'];
    $option_3 = $_POST['option_3'];
    $sql= "INSERT INTO php-startup (SNo, clientname, contactno, option_3)VALUES('SNo', 'clientname', 'contactno ','option_3')";

    if(!mysql_query($sql))
    {
        die('caution' . mysql_error());
    }
    echo"1 record added";
}

这将删除未定义的索引错误。并停止使用mysql_*语法,它们是depecrated。相反,请使用pdomysqli

进行学习

答案 2 :(得分:1)

mysql_connect dose将数据库名称作为参数see docs。 变化 - &GT;

mysql_connect($hostname, $username, $password, $dbname);

- &GT;

 $con = mysql_connect($host, $user, $pass);
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_select_db($database_name, $con)
    mysql_close($con);

我建议使用mysqli_connect see documentation

$link = mysqli_connect($host, $user, $password, $database_name);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
}