Jump to content

Recommended Posts

this is the code.

<?php

 

?>

                          <table align ="center" >

                            <form action="ManageTopics.php" method="POST">

                            <tr><td align="left" width=10>Tittle:</td>

                            <td align="left"><input name="title" type="text" size="25"  tabindex="1" /> </td></tr>

                            <tr><td align="left" width=10>Area:</td>

                            <td><input name="Area" type="text" size="25"  tabindex="2" /></td></tr>

                            <tr><td>Topic Description:</td></tr>

                            <tr><td COLSPAN="2"><textarea name="description" rows="15" cols="60"

 

tabindex="3"></textarea></td></tr>

                              <tr><td><input type="submit" name="CreateTopic" value="Create Topic" /></td></tr>

                            </form> </table>

<?php

$submit= $_POST['CreateTopic'];

if($submit){

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("cms", $con);

$TopicID=NULL;

$profId=10;

$title= $_POST['title'];

$Area= $_POST['Area'];

$Description= $_POST['description'];

$sql= "REPLACE INTO tbltopics

VALUES('$TopicID', '$title', '$Area', '$Description', '$profId')";

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "The topic Is Aded";

 

mysql_close($con);

}

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("cms", $con);

$query="SELECT Title FROM tbltopics";

$result=mysql_query($query);

$num=mysql_numrows($result);

echo "<b><center>Your Topics</center></b><br><br>";

$i=0;

while ($i < $num) {

$tittle=mysql_result($result,$i,"Title");

echo "<Table  align = center width='600'>";

$a=$i+1;

echo"<tr><td width='6'>$a</td><td width='100'>$tittle</td><td width='60'>View Description</td><td

 

width='70'>Delete</td></tr></b>";

$i++;

echo"</table>";

}

?>

 

 

And the problem is this:

When the forms ar empty and i hit the submit button it records one row in the database with empty fields only the Topic_Id field is recorded. How can I avoid this problem. I was trying to generate an error when forms are empty but id ont know how if someone nows something let me know or any other solution can be very helpfull for me.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/70757-solved-help-with-this-code/
Share on other sites

Just validate the data; check to see if anything is equal to an empty string. You may want to use trim() on your POST variables to make sure.

 

if ($title == "" ||  $Area == "" || $Description == "") {
   echo "Incorrect Data.";
} else {
    // Connect to db and insert row

}

Great Thanks man it works

here you have the fixed code

<?php

 

?>

                          <table align ="center" >

                            <form action="ManageTopics.php" method="POST">

                            <tr><td align="left" width=10>Tittle:</td>

                            <td align="left"><input name="title" type="text" size="25"  tabindex="1" /> </td></tr>

                            <tr><td align="left" width=10>Area:</td>

                            <td><input name="Area" type="text" size="25"  tabindex="2" /></td></tr>

                            <tr><td>Topic Description:</td></tr>

                            <tr><td COLSPAN="2"><textarea name="description" rows="15" cols="60"

 

tabindex="3"></textarea></td></tr>

                              <tr><td><input type="submit" name="CreateTopic" value="Create Topic" /></td></tr>

                            </form> </table>

<?php

$submit= $_POST['CreateTopic'];

if($submit){

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("cms", $con);

 

$profId=10;

$title= $_POST['title'];

$Area= $_POST['Area'];

$Description= $_POST['description'];

if ($title == "" ||  $Area == "" || $Description == "") {

  echo "Incorrect Data.";

} else {

$sql= "REPLACE INTO tbltopics

VALUES('$TopicID', '$title', '$Area', '$Description', '$profId')";

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "The topic Is Aded";

 

mysql_close($con);

}

}

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("cms", $con);

$query="SELECT Title FROM tbltopics";

$result=mysql_query($query);

$num=mysql_numrows($result);

echo "<b><center>Your Topics</center></b><br><br>";

$i=0;

while ($i < $num) {

$tittle=mysql_result($result,$i,"Title");

echo "<Table  align = center width='600'>";

$a=$i+1;

echo"<tr><td width='6'>$a</td><td width='100'>$tittle</td><td width='60'>View Description</td><td

 

width='70'>Delete</td></tr></b>";

$i++;

echo"</table>";

}

?>

 

very good.

 

one suggestion: there is no need to mess with mysql if the values aren't right. so you might want to move mysql_connect() and mysql_select_db() to after the } else {. only use them if the values are okay, otherwise no need to connect or select_db.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.