php - Prevent duplicate in inputting in MySQL db -


   <?php session_start(); require('../connection.php'); //if session isn't valid, returns login screen protection if(empty($_session['admin_id'])){  header("location:access-denied.php"); } //retrive positions tbpositions table $result=mysql_query("select * tbpositions") or die("there no records display ... \n" . mysql_error());  if (mysql_num_rows($result)<1){     $result = null; } ?> <?php // inserting sql query if (isset($_post['submit'])) {  $newposition = addslashes( $_post['position'] ); //prevents types of sql injection  $sql = mysql_query( "insert tbpositions(position_name) values ('$newposition')" )         or die("could not insert position @ moment". mysql_error() );  // redirect positions  header("location: positions.php"); } ?> <?php // deleting sql query // check if 'id' variable set in url  if (isset($_get['id']))  {  // id value  $id = $_get['id'];   // delete entry  $result = mysql_query("delete tbpositions position_id='$id'")  or die("the position not exist ... \n");    // redirect positions  header("location: positions.php");  }  else  // nothing  ?> <!doctype> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>administration control panel:positions</title> <link href="css/admin_styles.css" rel="stylesheet" type="text/css" /> <script language="javascript" src="js/admin.js"> </script> </head> <body bgcolor="tan"> <center><b><font color = "white" size="6">san francisco national high school <br> voting system</font></b></center><br><br> <div id="page"> <div id="header">   <h1>manage positions</h1>   <a href="admin.php">home</a> | <a href="positions.php">manage positions</a> | <a href="candidates.php">manage candidates</a> | <a href="refresh.php">poll results</a> | <a href="logout.php">logout</a> </div> <div id="container"> <table width="380" align="center"> <caption><h3>add new position</h3></caption> <form name="fmpositions" id="fmpositions" action="positions.php" method="post" onsubmit="return positionvalidate(this)"> <tr>     <td>position name</td>     <td><input type="text" name="position" /></td>     <td><input type="submit" name="submit" value="add" /></td> </tr> </table> <hr> <table border="0" width="420" align="center"> <caption><h3>available positions</h3></caption> <tr>  <th>position name</th>  </tr>  <?php //loop through table rows          mysql_connect("localhost", "root","") or die(mysql_error()); //connect server         mysql_select_db("poll") or die("cannot connect database"); //connect database         $result = mysql_query("select * tbpositions"); // sql query while ($row=mysql_fetch_array($result)){ echo "<tr>";  echo "<td>" . $row['position_name']."</td>"; echo "<td>" . $row['grade']."</td>"; echo "<td>" .$row['section']."</td>"; echo '<td align="center"><a href="#" onclick="myfunction('.$row['position_id'].')">delete</a> </td>'; echo "</tr>"; }         mysql_free_result($result); mysql_close($link);  ?> </table>  <script>       function myfunction(position_id)       {       var r=confirm("are sure want delete record?");       if (r==true)         {           window.location.assign("deletepositions.php?id=" + position_id);         }       }     </script> <hr> </div> <div id="footer">    <div class="bottom_addr">&copy; bsit 4th year</div> </div> </div> </body> </html> 

how can prevent duplicate entry database in position_name tried every example nothing works. please help. should do? want whenever user inputting duplicate entry give popup message , don't save. , when no duplicate save in database.enter image description here


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -