Reputation: 70
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?
<html>
<head>
<title>registration</title>
<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>
</head>
<body bgcolor=" #b3ffe0">
<style>
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;
}
</style>
</head>
<body>
<form action="df1.php" method="post">
<ul>
<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>
</ul>
<div>
<table border="2" align="center" style="border-spacing: 40px 30px;">
<caption><strong>DISEASE DETAILS:</br></br></strong></caption></br></br>
<tr>
<td><center> Disease_ID
<select name="Day">
<option value="1">1</option>
<option value="2">2</option>
</select></center>
</td>
<td>Disease
<select name="DisType">
<option value="Select">Select</option>
<option value="Acute">Acute</option>
<option value="Chronic">Chronic</option>
<option value="Acquired">Acquired</option>
</select>
</td>
<td>SubDisease
<select name="DisType">
<option value="Select">Select</option>
<option value="Acute">Acute</option>
<option value="Chronic">Chronic</option>
</select>
</td>
<td>Associated_Disease<input type="text" name="DisDu"></td>
</tr>
<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>
</select>
</td>
<td>Source<textarea name="comments" cols="30" rows="4"></textarea><br></td>
</tr>
</table>
</div>
</br>
<div><center>
<input type="submit" name="submit">
</center></div></div>
</form>
<?php
if(isset($_POST['submit'])){
$conn = mysqli_connect('localhost','root','');
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
echo "DB Connected successfully";
mysqli_select_db("tool",$conn);
$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]')";
mysqli_query($sql,$conn);
mysqli_close($conn);
}
?>
</body>
</html>
Upvotes: 0
Views: 92
Reputation: 22760
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:
mysqli_query($conn,$sql);
4) $conn = mysqli_connect('localhost','root','');
should also reference the correct database, again, simple stuff - read the manual!
So:
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:
Example:
<form method="post">
<input value="whatever" name="myName">
</form>
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.
Upvotes: 1
Reputation: 816
your post indexes are wrong you can not use your drop down title in that, you need to use value of your name attribute of input field so $_POST['Day']
instead of $_POST['Disease_ID']
, use $_POST['DisDu']
instead of $_POST[Associated_Disease]
and so on for other inputs.
Upvotes: 1
Reputation: 23379
If you're ok with leaving yourself open to SQL injection and getting hacked, go ahead and follow this advice. If not, you better read up on prepared statements instead..
1) Unless your config file lists the database, you need to specify it in your constructor:
$conn = mysqli_connect('localhost','root','', 'myDatabaseName');
2) When using arrray indexes in a string you should wrap them in curlies:
$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]}')";
Upvotes: 2