Jump to content

check if already exist before creating a new row


anarchoi

Recommended Posts

ok i have this part of script, that i found in a tutorial and modified it

 

basically it's a news poster. So it creates a new row for each news.

 

I'd like to check if the title of the news is already present in the database ("title" in database) and if so, don't post the new and display error message

 

could anyone help me doing this? i'm kinda newbie

 

 

here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<?

//initilize PHP

 

if($_POST['submit']) //If submit is hit

{

  //then connect as user

  //change user and password to your mySQL name and password

  mysql_connect("localhost","anarchoi","*********");

 

  //select which database you want to edit

  mysql_select_db("anarchoi1_phpb1");

 

  //convert all the posts to variables:

  $title = $_POST['title'];

  $message = $_POST['message'];

  $who = $_POST['who'];

  $date = $_POST['date'];

  $time = $_POST['time'];

 

  //Insert the values into the correct database with the right fields

  //mysql table = news2

  //table columns = id, title, message, who, date, time

  //post variables = $title, $message, '$who, $date, $time

  $result=MYSQL_QUERY("INSERT INTO news2 (id,title,message,who,date,time)".

      "VALUES ('NULL', '$title', '$message', '$who', '$date', '$time')");

 

    //confirm

  echo "Query Finished";

}

else

{

// close php so we can put in our code

?>

<form method="post" action="add.php">

<TABLE>

<TR>

  <TD>Title:</TD>

  <TD><INPUT TYPE='TEXT' NAME='title' VALUE='Random Update' size=60></TD>

</TR>

<TR>

  <TD>Message:</TD>

  <TD><INPUT TYPE='TEXT' NAME='message' VALUE='' size=60></TD>

</TR><br>

<TR>

  <TD>Name:</TD>

  <TD>

      <SELECT NAME='who'>

        <OPTION VALUE='Akash'>Akash

        <OPTION VALUE='Brian'>Brian

      </SELECT>

  </TD>

</TR>

<TR>

  <TD>Date:</TD>

  <TD>

      <!-- You can use PHP functions to automatically get the value of date -->

      <INPUT TYPE='TEXT' NAME='date' VALUE='<? echo date("M.j.y"); ?>' size=60>

  </TD>

</TR>

<TR>

  <TD>Time:</TD>

  <TD>

      <!-- You can use PHP functions to automatically get the value of time -->

      <INPUT TYPE='TEXT' NAME='time' VALUE='<? echo date("g:i a"); ?>' size=60>

  </TD>

</TR>

<TR>

  <TD></TD><br>

  <TD><INPUT TYPE="submit" name="submit" value="submit"></TD>

</TR>

</TABLE>

</form>

 

<?

} //close the else statement

?>

 

</body>

</html>

this is the query ur  lookng 4...?

if($_POST['submit']) //If submit is hit

{

  //then connect as user

  //change user and password to your mySQL name and password

  mysql_connect("localhost","anarchoi","*********");

 

  //select which database you want to edit

  mysql_select_db("anarchoi1_phpb1");

 

  //convert all the posts to variables:

  $title = $_POST['title'];

  $message = $_POST['message'];

  $who = $_POST['who'];

  $date = $_POST['date'];

  $time = $_POST['time'];

  $strSQL = "SELECT * FROM  news2 WHERE title='".$title."'";

$rs=mysql_query($strSQL); //execute the query

  if(mysql_num_rows($rs)==1)

{

$as = rand(0,9);

echo "Title Already Exists";

}

else

{

$result=MYSQL_QUERY("INSERT INTO news2 (id,title,message,who,date,time)".

      "VALUES ('NULL', '$title', '$message', '$who', '$date', '$time')");

 

    //confirm

  echo "Query Finished";

}

 

 

  //Insert the values into the correct database with the right fields

  //mysql table = news2

  //table columns = id, title, message, who, date, time

  //post variables = $title, $message, '$who, $date, $time

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.