Jump to content

pls am a beginer


shashidharkulkarni

Recommended Posts

i am trying to put form data to mysql table

what's wrong with my code below does not give any error but not inserting any data in table

<html>
<head>
<title>HTML formS</title>
</head>
<body>
<form action="insertform.php" method="post">
Name : <input type="text" name="name"><br>
address : <input type="text" name="address"><br>
topic :<input type="text" name="topic1"><br>
<input type="submit" value="submit">
</form>
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$topic1 = $_POST['topic1'];
 
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","db","password");
if (!$con){
die("can not connect".mysql_error());
}
mysql_select_db("db",$con);
$sql= "INSERT INTO student_register_test10 (name, address, topic1) values ($name, $address, $topic1)";
mysql_query($sql,$con);
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Success!";
mysql_close($con);
}
?>
</body>
</html>
 
Link to comment
Share on other sites

You have several issues.

 

First, you are using deprecated code. You need to use PDO with prepared statements.

 

Next, you are setting post variables outside of your isset post check so you have errors there.

 

If you turned on error reporting you would see that as well as any other errors

 

You are also mixing mysql with mysqli. You cant do that.

 

Your code is also vulnerable to SQL Injection. You NEVER EVER send user supplied data directly to the database.

 

You need to rewrite the whole thing. None of your code is any good.

Edited by benanamen
Link to comment
Share on other sites

And you attempt to access something named 'submit' in the POST array.  You didn't assign a name= to your submit button so you have none.

 

For future reference (your next post?) - it's a better idea to actually indicate what you want in the subject/topic you post under.  Naming your post as you did here is not much help in getting the attention you seek, other than from the curious who have the time to browse everything.  Better pinpoint a topic that you wish help on than hope for someone's curiosity to cause them to read your question.

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.