Jump to content

nothing happens when I click the submit button to insert


ricky15

Recommended Posts

I have this code the form is set to action on itself but when I click the submit button nothing happens no error and no data is inserted into the mysql 

 

 

<html>

<head>

</head>

<title>stock</title>

<body>

<form action="st.php" method="post">

<input name="code" type="text" >

<input name="name" type="text">

<input name="submit" type="submit" >

</form>

<?php

 

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

 

$link = mysql_connect("localhost","root","");

if(!$link){

die("cant connect" . mysql_error());

}

 

mysql_select_db("test",$link);

 

$sql= "INSERT INTO stock (sid,stcode,stname) VALUES ('','$_POST
','$_POST[name]')";
 

mysql_query($sql,$link);

 

mysql_close($link);

}

?>

</body>

</html>

 

Link to comment
Share on other sites

In addition to checking for PHP errors, as suggested by ginerjm, you may also need to see if MySQL is throwing errors. To see if the query failed, for example, you could use the following:

$sql= "INSERT INTO stock (sid,stcode,stname) VALUES ('','$_POST[code]','$_POST[name]')";
mysql_query($sql,$link) or die(mysql_error());

 
And just to clarify what benanamen mentioned, the mysql_* functions were removed in PHP 7.0. So your code needs to be updated in the very near future. More information about the alternatives can be found here:
http://php.net/manual/en/mysqlinfo.api.choosing.php
 
As for the security aspect, use caution when dealing with data that users can tamper with, such as the information collected in an HTML form. The code posted, for example, is susceptible to SQL injection attacks. To protect yourself, with the old mysql_* functions, you can use the mysql_real_escape_string() function. More information can be found here:

 
When switching to PDO (or MySQLi), you will want to look into prepared statements.  :happy-04:
Edited by cyberRobot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.