Steffi Ma Steffi Ma - 2 years ago 58
SQL Question

Inserting data into database in php and mysql

I've tried the following code for inserting data into the database. The database is getting connected. But the data is not getting added to the database. I dont know where i have gone wrong. Can anyone help me with this?


<meta charset="UTF-8">
<link href="site.css" rel="stylesheet">
<div align="center">
<link rel="stylesheet" href="mine.css"/>

<table border="0" align="center" style="border-spacing: 40px 20px;">

<align="center"> <td>

<body bgcolor=" #b3ffe0">


html {
font-family: "Lucida Sans", sans-serif;
ul li {display: block;position: relative;float: left;border:1px }
ul li a {display: block;text-decoration: none; white-space: nowrap;color:#fff;}
ul {
list-style-type: none;
padding: 2px ;
margin-left: auto;
background-color: #666;
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 10px 20px;
text-decoration: none;

li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
<form action="df1.php" method="post">
<li><a class="active" href="df.html">Disease</a></li>
<li><a href="drug.html" align="_self">Drug</a></li>
<li><a href="#about">Interaction</a></li>
<a href="#" class="dropbtn">Alternate Drug</a>


<table border="2" align="center" style="border-spacing: 40px 30px;">
<caption><strong>DISEASE DETAILS:</br></br></strong></caption></br></br>
<td><center> Disease_ID
<select name="Day">
<option value="1">1</option>
<option value="2">2</option>
<select name="DisType">
<option value="Select">Select</option>
<option value="Acute">Acute</option>
<option value="Chronic">Chronic</option>
<option value="Acquired">Acquired</option>

<select name="DisType">
<option value="Select">Select</option>
<option value="Acute">Acute</option>
<option value="Chronic">Chronic</option>


<td>Associated_Disease<input type="text" name="DisDu"></td>

<td>Ethinicity<select name="DisType">
<option value="Select">Indian</option>
<option value="Acute">European</option>
<option value="Chronic">oman</option>
<option value="Acquired">German</option>


<td>Source<textarea name="comments" cols="30" rows="4"></textarea><br></td>

<input type="submit" name="submit">
$conn = mysqli_connect('localhost','root','');
if (!$conn) {
die("Connection failed: " . mysqli_error());
echo "DB Connected successfully";
$sql="INSERT INTO disease (Disease_id, Disease,SubDisease, Associated_Disease, Ethinicity,Source)
VALUES ('$_POST[Disease_ID]', '$_POST[Disease]','$_POST[SubDisease]', '$_POST[Associated_Disease]','$_POST[Ethinicity]', '$_POST[Source]')";

Answer Source

1) Disease_ID should be an auto-incremental primary index column in your MySQL so should not be referenced directly by the HTML output. When inserting a row in the database this value will be self-generating and unique.

2) Check your syntax. Your error log should be mysqli_error($conn) <== you need to specify the connection variable. This goes for most actions using MySQLi procedural. Ask yourself how does the command know which database to apply the action to?

3) mysqli_query($sql,$conn); This is the primary cause of your issue.

The correct syntax is:


Read the manual.

4) $conn = mysqli_connect('localhost','root',''); should also reference the correct database, again, simple stuff - read the manual!


mysqli_connect('localhost','root','', 'tool');

And delete your mysqli_select_db reference in the code.

5) Add mysqli_error to your query insert so:

mysqli_query($conn,$sql) or die("error: ".mysqli_error($conn));

6) Secondary Cause - Your form submits data with the name attribute, buy all your form data seems to be subitted with the same name, so the data is not being received by the PHP code:


<form method="post">
<input value="whatever" name="myName">

And PHP recieves:

$_POST['myName'] = "whatever"; 

You need to update your whole HTML form with this in mind as currently your PHP is referencing value which do not exis and your HTML form is only posting a few unique values.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download