我在此代码中缺少什么? [英] What am I missing from this code?

查看:78
本文介绍了我在此代码中缺少什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP和MySQL的初学者,我目前正在设计一个网站进行作业.该网站应允许您查看数据库,从数据库中添加和删除记录.

Hi I am a beginner in PHP and MySQL, I am currently designing a website for an assignment. The website should allow you to view the database, add and delete records from the database.

下面的代码只是创建一个用于添加记录的表单页面,但实际上不能将记录添加到数据库中.

The following code simply creates a form page for adding records but cannot actually add a record to the database.

任何人都可以告诉我我所缺少的内容,我需要在代码中添加的内容,应该在哪里添加以及应该做的任何更改.

Can anyone Please tell me what I am missing, what I need to add to the code and where it should be added and also any changes I should make.

<html>
    <head>
        <title>New Record</title>
    </head>
    <body>

<?php
if(isset($_POST["ID"])) {
    $ID = $_POST['ID'];
    $ProductName = $_POST['ProductName'];
    $Price = $_POST['Price'];
    $Stock = $_POST['Stock'];
}

$error='';
if ($error != '');
{
    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

        <form action="New.php" method="post">
            <div>
                <strong>ID: </strong> <input type="int" name="ID"><br>
                <strong>ProductName: </strong> <input type="VARCHAR" name="ProductName"><br>
                <strong>Price:  </strong> <input type="text" name="Price"><br>
                <strong>Stock:  </strong> <input type="int" name="Stock"><br>
                <input type="submit" name="submit" value="Submit">
            </div>
        </form>
    </body>
</html>

<?php
$con = mysqli_connect("localhost","root","");
if (!$con) 
{
    mysqli_select_db("stationaryonlinecustomers", $con);
}

if (isset($_POST['submit']))
{
    $ID = mysqli_real_escape_string($con, htmlspecialchars($_POST['ID']));
    $ProductName = mysqli_real_escape_string($con,htmlspecialchars($_POST['ProductName']));
    $Price = mysqli_real_escape_string($con,htmlspecialchars($_POST['Price']));
    $Stock = mysqli_real_escape_string($con,htmlspecialchars($_POST['Stock']));
}

$ID='';
if ($con == '' || $ID == '' || $ProductName == '' || $Price == '' || $Stock =='') {
    $error = 'ERROR: Please fill in all required fields!';
}
else {
    $u = "INSERT INTO productorders (ID, ProductName, Price, Stock)
    VALUES
    ('$_POST[ID]','$_POST[ProductName]','$_POST[Price]','$_POST[Stock]')";
}

header("refresh:100;View.php");
?>

推荐答案

您可以在New.php文件中编写php代码,因为您在action.form表单中提到了New.php文件名,因此请创建New.php文件并添加以下代码

you can write php code in New.php file because you have mentioned New.php file name in form action.so create New.php file and add following code

New.php

<?php

 $con = mysqli_connect("localhost","root","");
 if (!$con) 
 {
     mysqli_select_db("stationaryonlinecustomers", $con);
 }

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

{

$ID = mysqli_real_escape_string($con, htmlspecialchars($_POST['ID']));

$ProductName = mysqli_real_escape_string($con,htmlspecialchars($_POST['ProductName']));

$Price = mysqli_real_escape_string($con,htmlspecialchars($_POST['Price']));

$Stock = mysqli_real_escape_string($con,htmlspecialchars($_POST['Stock']));

}


$ID='';
if ($con == '' || $ID == '' || $ProductName == '' || $Price == '' || $Stock =='')

{
$error = 'ERROR: Please fill in all required fields!';

}

else{

$u = "INSERT INTO productorders (ID, ProductName, Price, Stock)
VALUES
($ID,$ProductName,$Price,$Stock)";

}

header("refresh:100;View.php");

?>

我修改了插入查询,您可以在上面的代码中看到一次

I have modified the insert query you can see that once in the above code

这篇关于我在此代码中缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆