php - Updating data in mysql table -


i trying see if keyword exists in table searchedwords. if does, countr increases one. if not exists in table, used insert. problem is, keyword being passed site not stored in db. other big problem countr not add at. because of if statement? or while loop?

<?php date_default_timezone_set('asia/manila'); $today = date('m-d-y'); echo $today; $urltopost = "http://opac.usls.edu.ph/tlcscripts/interpac.dll?search"; $datatopost = "formid=0&config=pac&limitsid=0&startindex=0&searchfield=7&searchtype=1&itemsperpage=20&searchdata=$_post[keyword]"; $ch = curl_init ($urltopost); curl_setopt ($ch, curlopt_post, 1); curl_setopt ($ch, curlopt_postfields, $datatopost); curl_setopt ($ch, curlopt_header, 0); curl_setopt ($ch, curlopt_returntransfer, 1); $returndata = curl_exec ($ch); echo $returndata; $con=mysqli_connect("...","...","...","...")or die ('error: ' . mysql_error()); $sql= "select * searchedwords"; $result= mysqli_query($con,$sql);    while($row=mysqli_fetch_array($result, mysqli_assoc)) {                     if($row['keyword']==$_post[keyword])          {          $upd="update searchedwords set countr = countr + 1 keyword = '".$row['keyword']."'";           while (!mysqli_query($con,$upd))           {            die('error: ' . mysqli_error($con));            }          }         else         {          $insertin="insert `searchedwords`( `keyword`, `countr`) values ('$_post[keyword]',1)";       while (!mysqli_query($con,$insertin))           {           die('error: ' . mysqli_error($con));           }         }  } ?> 

thank can me out.

why making things complex... try simple way

$keyword = $_post['keyword']; //do not forget sanitize security $sql= "select * searchedwords keyword='$keyword'"; $result= mysqli_query($con,$sql); $count= mysqli_num_rows($result);  if($count) { $upd="update searchedwords set countr = countr + 1 keyword = '$keyword'"; mysqli_query($con,$upd); } else { $insertin="insert `searchedwords`( `keyword`, `countr`) values ('$keyword',1)"; mysqli_query($con,$insertin); } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -