数据已插入我想更新数据

时间:2015-07-06 17:45:22

标签: php html mysql sql

我在更新代码中遇到问题。我能够在数据库中插入数据。我在表中传递空值。我想更新那些空值。我收到了很多消息,但数据没有更新。请帮帮我......

    //Insert code


<?php
// Start the session
session_start();
?>

<?php
// Start the session
session_start();
?>


<?php


try{

$product=$_POST['product'];
/*
$product2=$_POST['product2'];
$product3=$_POST['product3'];
*/
    // form data





    //database Connection details
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database="store";
    $error = "";

    $conn=mysql_connect($servername, $username, $password) or die('Connection failed: ' . mysql_error());

    @mysql_select_db($database, $conn) or die("Could not select your database".mysql_error());


        $insertQuery = "Insert into contactus(Id,Product) values('null','$product')";

        $result = mysql_query($insertQuery);

        if($result){
            echo "<script>alert('Thank You. Your Data Received Succefully.');location.href = '../index.html';</script>";
        }
        else
        {
           echo "<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../index.html';</script>"; 
        }


    mysql_close($conn);
    header('Location: /newstore/contact.html');   

}

catch(Exception $e) {
    echo ("<script>alert('Something went terribly wrong. Please try again later.');location.href = ''../index.html';</script>");
    return false;
}

?>

    //Update code

    <?php
// Start the session
session_start();
?>


<?php


try{

    // form data
    $name=$_POST['name'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];
    $product=isset($_POST['product']);


    //database Connection details
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database="store";
    $error = "";

    $conn=mysql_connect($servername, $username, $password) or die('Connection failed: ' . mysql_error());

    @mysql_select_db($database, $conn) or die("Could not select your database".mysql_error());
;if ((strlen($name) < 3) or (strlen($email) < 3) or(strlen($mobile) < 3))
{
    echo ("<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../newstore/index.html';</script>");
}else
{



    $UpdateQuery = "update contactus set Name='$name',Email='$email',Mobile='$mobile' where Id='(select count(*) from contactus)' ";

        $result = mysql_query($UpdateQuery);

        if($result){
            echo "<script>alert('Thank You. Your Data Received Succefully.');location.href = '../newstore/index.html';</script>";
        }
        else
        {
           echo "<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../newstore/index.html';</script>"; 
        }
    }

    mysql_close($conn);
}

catch(Exception $e) {
    echo ("<script>alert('Something went terribly wrong. Please try again later.');location.href = ''../newstore/index.html';</script>");
    return false;
}

?>

1 个答案:

答案 0 :(得分:0)

我认为执行Insert然后执行更新没有意义。您已拥有所有数据,因此只需一次插入即可。

评论后编辑

First Handler:

<?php
start_session();

if(isset($_POST['product'])){
    $product=$_POST['product'];

    //database Connection details
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database="store";
    $error = "";

    $mysqli = new mysqli($servername, $username, $password, $database);
    /* check connection */
    if (mysqli_connect_errno()) {
        echo "<script>alert('Something went wrong with your data inserted. Please fill the form again. (" .  mysqli_connect_error() . ")');location.href = '../newstore/index.html'</script>");
        exit();
    }
    if ($result = $mysqli->query("INSERT INTO contactus (Id,Product) VALUES ('null','$product')")) {
        // Grab new ID when INSERT is successfull, add it to Session
        $_SESSION['contact_id'] = $mysqli->insert_id;
        echo "<script>alert('Thank You. Your Data Received Succefully.');location.href = '../index.html';</script>";
    } else {
        echo "<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../index.html';</script>";
        $mysqli->close();
        exit();
    }
    $mysqli->close();
}
header('Location: /newstore/contact.html');
?>

Second Handler:

<?php
start_session();
// form data
$name=isset($_POST['name'])?$_POST['name']:"";
$email=isset($_POST['email'])?$_POST['email']:"";
$mobile=$_POST['mobile'];
if ((strlen($name) < 3) || (strlen($email) < 3) || (strlen($mobile) < 3)){
    echo "<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../newstore/index.html';</script>";
    exit();
}

//database Connection details
$servername = "localhost";
$username = "root";
$password = "";
$database="store";
$error = "";

$mysqli = new mysqli($servername, $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
    echo "<script>alert('Something went wrong with your data inserted. Please fill the form again. (" .  mysqli_connect_error() . ")');location.href = '../newstore/index.html'</script>");
    exit();
}

if ($stmt = $mysqli->prepare("UPDATE contactus SET `Name`=?, `Email`=?, `Mobile`=?) WHERE `ID`=?")){
    /* bind parameters for markers */
    $stmt->bind_param("sssi", $name, $email, $mobile, $_SESSION['contact_id']);
    /* execute query */
    $stmt->execute();
    $result = $stmt->get_result();
    if($result){
        echo "<script>alert('Thank You. Your Data Received Succefully.');location.href = '../newstore/index.html';</script>";
    } else {
        echo "<script>alert('Something went wrong with your data inserted. Please fill the form again.');location.href = '../newstore/index.html';</script>"; 
    }
    $stmt->close();
}
$mysqli->close();
?>