my insert php cannot save data to phpmyadmin when it enter the details.
localhost:locahost , username:root password: databasename:b_database tablename:my_library
tablecolumn:2 contains : isbn(primary key) and title.

index.php

<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Library Record</title>
</head>
<body>
<center><h1><u>Library Database</u></h1></center>
<form name="search" method="post" action="search.php">
<table style=" border:1px solid silver" cellpadding="10px" cellspacing="0px"
align="center">
<tr>
<td colspan="3" style="background:#0066FF; color:#FFFFFF; fontsize:
20px">Search</td></tr>
<tr>
<td>Enter Search Keyword</td>
<td><input type="text" name="search" size="40" /></td>
<td><input type="submit" value="Search" /></td>
</tr>
<tr>
<td colspan="3"> </td></tr>
<tr bgcolor="#CCCCCC">
<th><a href="add.php">Add Record</a></th>
<th><a href="del.php">Delete Record</a></th>
<th><a href="del.php">Update Record</a></th>
</tr>
</table>
</form>
</body>
</html>

add.php

<?php
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db='b_database';

if(!@mysql_connect($mysql_host, $mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
  die($conn_error);

}
?>

<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Student Record</title>
</head>
<body>
<center>
<h1><u>Library Database</u></h1>
</center>
<?
if($_POST["do"]=="store")
{
$isbn=$_POST["isbn"];
$title=$_POST["title"];
if($query_run = mysql_query($query)){
  $query="insert into mylibrary value('$isbn','$title')";
  mysql_query($query);
  echo "Successfully store in DATABASE";
  }
  ?>
  <form name="add" method="post" action="add.php">
  <table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px"
  align="center" border="0">
  <tr>
  <td colspan="4" style="background:#0066FF; color:#FFFFFF; fontsize:
  20px">ADD RECORD</td>
  </tr>
  <tr>
  <tr>
  <td>Enter ISBN</td>
  <td><input type="text" name="isbn" size="20"></td>
  </tr>
  <tr>
  <td>Enter TITLE</td>
  <td><input type="text" name="title" size="20"></td>
  </tr>
  <tr>
  <td colspan="4" align="center"><input type="hidden" name="do" value="store">
  <input type="submit" value="ADD RECORD"></td>
  </tr>
  </table>
  </form>
  <p align="center"><a href="index.php">Go Back to Home</a></p>
  <?
  include("search.php");?>
  </body>
  </html>

Hi,

Although this codes below works, it contradict its own conditions.. Assuming that suppressant will work on mysql_connect and select, this should not be written like this..

if(!@mysql_connect($mysql_host, $mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
die($conn_error);
}

Why is there a contradiction? Because you are suppressing it and yet you want it to return the error "could not Connect" on die function.

This type of coding implementation should only be use in OOP where other methods can validates if the connection exists. Otherwise, if it is in procedural..I totally agree with the notion of simplicity makes everything works with less frills.

I strongly suggests to change the above codes to something simple as this

mysql_connect($mysql_host, $mysql_user,$mysql_pass) or die(mysql_error($conn_error));
mysql_select_db($mysql_db) or die(mysql_error('Could not Connect to database table'));

The same is true with the insert query. Everyone should start where the wonderful basic coding standards reign, and then we create a more complex and extensible codes, once we are applauded by our peers in doing the basics.

if($query_run = mysql_query($query)){
$query="insert into mylibrary value('$isbn','$title')";
mysql_query($query);
echo "Successfully store in DATABASE";
}

Why is the above wrong? Because you do not need to assign the query funtion to a variable. If you want to do it with if statement, then just hit straight to the bottom of it like..

if(mysql_query($query)){
echo "It's done";
}

You codes above is totally wrong.. and here is the way of doing it in the sense of being plain as we call it as basic construct.

mysql_query("INSERT INTO example (isbn, title) VALUES('". $isbn ."', '". $title ."' ) ") 
or die(mysql_error('OOPS!. something is wrong'));  

echo 'Successfully stored in the DATABASE';

if you want with if statement then you can literally do it like this

$query = ("INSERT INTO example (isbn, title) VALUES('". $isbn ."', '". $title ."' ) ");

if(mysql_query($query){
echo 'Successfully stored in the Database';
}

else{
## you decide

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.