简体   繁体   中英

Connecting Php, html, and mysql

Can someone look over my code and let me know what's wrong with it?

The problem I'm having is that when I enter text to the 3 fields and hit submit, it doesn't insert to my database (mysql, with phpmyadmin as gui). No error messages or anything; it simply doesn't insert the data..

I have looked over the code over and over, and I can't pin point what's wrong with it.

//---------------------------This is my index.php------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Web Bar Title</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>


<?php
if(isset($_POST['Submit']))
{
   include 'connectdb.php';
   include 'openconnection.php'; 

    $first = $_POST['first'];
    $second = $_POST['second'];
    $third = $_POST['third'];

    $query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";
    mysql_query($query) or die('Error, insert query failed');
}
?>

        <div id="page">

            <tbody>
            <form method="post">
                <table>
                <tr>
                <td ><b>First</b></td>
                <td><input name="first" type="text" id="first"></td>
                <tr>
                <tr>
                <td ><b>Second</b></td>
                <td><input name="second" type="text" id="second"></td>
                <tr>
                <td ><b>Company</b></td>
                <td><input  name="third" type="text" id="third" >  </td>
                </tr>
                </table>
            <input name="submit" type="submit" id="submit" value="Submit" />
            </form>
            </body>
            </html>
            </tbody>

        </div>

//---------------------------------connectdb.php------------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>

//---------------------------------openconnection.php-------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname) or die ('No Selection of Database');
?>

EDIT: It would be easier and faster to communicate via MSN/AIM/Steam/Skype, if anyone has any of those!

Change your top line to

if( isset( $_POST['submit'] ) ) {

I can't remember if it is case sensitive or not

Better still change it to

if($_SERVER['REQUEST_METHOD'] == 'POST')

The isset() method on the submit button is unreliable because Internet Explorer will not send the submit button as a post variable if the user presses the enter key to submit the form, and thus your code will not detect a form submission.

Just check MYSQL INSERT query in Your own MySQL platform, by simply copying your INSERT query and paste in your MySQL database, and check there. it wil report your fault.

If I'm right, then your problem lies on your INSERT query part. You have stated :

$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";

In the above part, there shouldn't be 2 semicolons, Jus one is enough... it wil look like :

$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third')";

Also, check ur include part too...

it shouldnt be :

include 'connectdb.php';  //braces r missing
include 'openconnection.php';  // braces r missing 

it should be :

include ('connectdb.php');
include ('openconnection.php'); 

I hope this may do good fa U ....

CHEERS buddy.........

尝试将值放在双引号之间而不是单引号

VALUES("$first","$second","$third")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM