douzhao6584 2016-11-23 10:12
浏览 24

HTML发布到PHP

Trying to insert data into my table but I keep getting an undefined index because there are "no value set when I submit my form". The if (isset($_POST['submit'])) removes my error even when I run the .php alone but no data is inserted when I submit my form. Any help is appreciated. Thank you

My form.html

<form name="supportForm" class="form" action="database.php" method="POST" onsubmit="return validateForm()">

<label>Name:</label>
<input type="text" name="name"/>
<br/>

<label>Client ID:</label>
<input type="text" name="clientID"/>
<br/>

<label>E-mail address:</label>
<input type="email" name="email"/>
<br/>

<label>Phone number:</label>
<input type="tel" name="tel"/>
<br/>

<br/>
Support Type:<br>
<input type="radio" name="suppType" value="Question/Inquiry">Question/Inquiry<br>
<input type="radio" name="suppType" value="Software">Software Issue<br>
<input type="radio" name="suppType" value="Hardware">Hardware Issue<br>
<input type="radio" name="suppType" value="Connectivity">Connectivity<br>
</br>

Operating System:
<select id="select">
    <option disabled selected value="">Choose a product</option>
    <option value="w7" name="OS">Windows 7</option>
    <option value="w8" name="OS">Windows 8/8.1</option>
    <option value="w10" name="OS">Windows 10</option>
</select>

<br> </br>

Problem Description:
<br><textarea id="ta" rows="10" cols="80" name="pDesc"></textarea></br>

<input type="checkbox" name="terms" value="agree">
<a href="#">I agree to the terms and conditions.</a>

<br> </br>
<input type="hidden" name="submitted" value="true">
<input type="submit" name="submit" onClick="validateSubmit()">
</form>

My PHP file

<?php
//Creates static credentials
define('DB_NAME', 'data');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

//Creates connection to the database
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);

//Checks for connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

//If there are no connection, error
if (!$con) {
    die ('Could not connect' . mysqli_error());
}

//Select the 'data' database
$con->select_db(DB_NAME);

//Checks if database 'data' has been selected
if (mysqli_select_db($con, DB_NAME)) {
    echo "Database exists <br>";
} else {
    echo "Database does not exist";
}

//Successful connection message
echo "Connected successfully <br>";

if (isset($_POST['submit'])) {
//Retrieving values from support form
    $name = $_POST['name'];
    $clientID = $_POST['clientID'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $suppType = $_POST['suppType'];
    $OS = $_POST['OS'];
    $pDesc = $_POST['pDesc'];

//Inserting values into a table
    $sql = "INSERT INTO info (fullname, clientID, email, tel,
    suppType, OS, pDesc)
    VALUES ($name, $clientID, $email, $tel, 
    $suppType, $OS, $pDesc)";

if (!mysqli_query($con, $sql)) {
    echo "No data";
} else {
    echo "Data recorded successfully";
}
}

//Closes connection
mysqli_close($con);
  • 写回答

2条回答 默认 最新

  • dsgdhtr_43654 2016-11-23 10:24
    关注

    You must write name="OS" in <select> not in <option>

    <select id="select" name="OS">
        <option disabled selected value="">Choose a product</option>
        <option value="w7">Windows 7</option>
        <option value="w8">Windows 8/8.1</option>
        <option value="w10">Windows 10</option>
    </select>
    

    And Sql must be like this you need apostrophes ('') around variables

    $sql = "INSERT INTO `info` (fullname, clientID, email, tel, suppType, OS, pDesc)
    VALUES ('$name', '$clientID', '$email', '$tel', '$suppType', '$OS', '$pDesc')";
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)