我的PHP代码中缺少什么? [英] What am I missing in my PHP code?

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

问题描述

您好我是PHP和MySQl的初学者,我正在建立一个可以查看数据库,向数据库添加数据和从数据库中删除数据的网站。我已经为表单页面编写了代码,允许用户将数据添加到数据库。



代码不包含任何错误,表单出现,你可以输入详细信息,然后单击提交按钮,没有任何问题。但它实际上并没有将输入的信息添加到数据库中。



这是我的New.php的代码













Hi I am a beginner in PHP and MySQl, I am making a website that can view a database, add data to database and delete data from a database. I have written code for a form page which allows the user to add data to the database.

The code doesn't contain any errors, the form appears, you can enter the details and click the submit button with no issues. But it does not actually add the information entered to the database.

Here is the code for my "New.php"






<title>New Record

	

	
		
	<?php
     if(isset($_POST["ID"])){
	
    $ID = $_POST['ID'];
	$ProductName = $_POST['ProductName'];
	$Price = $_POST['Price'];
	$Stock = $_POST['Stock'];
	 }
	
	// if there are any errors, display them
    $error='';
	if ($error != '');
	 
	{

	echo '<div style="padding: 4px; color: red">'.$error.'</div>';
//if assist
	}

	?>

	

	<div>
	   
	ID:  <br>

	ProductName:  <br>

	Price:   <br>
	
	Stock:   <br>

	

	</div>

	

	

	

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

	
	// check if the form has been submitted. If it has, start to process the form and save it to the database

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

	{

	// get form data, making sure it is valid

	$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']));
      
	}

	// check to make sure both fields are entered
    $ID='';
	if ($con == '' || $ID == '' || $ProductName == '' || $Price == '' || $Stock =='')

	{

	// generate error message

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

	else{
	// save the data to the database

	$u = mysql_query($con, "INSERT productorders SET ID='".$ID."', ProductName='".$ProductName."', Price='".$Price."', Stock='".$Stock."'");
	

	// once saved, redirect back to the view page

	header("location:View.php");


	// if the form hasn't been submitted, display the form

	renderForm('','','');

	}

	?></



我缺少什么?



我的尝试:



Youtube教程,来自不同网站的教程以及关于PHP和MySql的书籍


What am I missing ?

What I have tried:

Youtube tutorials, Tutorials from different websites and books on PHP and MySql

推荐答案

_POST [ ID])){

_POST["ID"])){


ID =
ID =


_POST [' ID'];
_POST['ID'];


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

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