Hello friends,
Iam Viru, and iam new to the PHP
i want to add list of categories in that,
but iam unable to add those,
but iam not getting any error message also
can any one help me please.
is there any error in these please.

Thank u in Advance

<?
include"functions.php";
include"conn.php";
if($_SERVER['REQUEST_METHOD']=='POST')
{ if(!empty($_GET['id']))
{
$cond="and id<>'".$_GET['id']."'";
}
else
$cond = "";
$sqry="select count(*) as cnt from category where catname='".$_POST['catname']."'".$cond;
$sres=mysql_query($sqry);
$srow=mysql_fetch_assoc($sres);
if($srow['cnt']>0)
{
$dup_msg="Category with this name already exists";
}
else if(empty($_GET['id']))
{ $qur=mysql_query("INSERT INTO `categories` (`catname` , `parentid`) VALUES ('".$_POST['catname']."','0')");
echo '<script language="javascript"> window.location.href="addcat.php?msg=1";</script>';
}
else
{ $qur=mysql_query("UPDATE `categories` SET `catname` = '".$_POST['catname']."' WHERE `id` =".$_GET['id']);
echo '<script language="javascript"> window.location.href="managecat.php?msg=2";</script>';
}
}
if(!empty($_GET['id']))
{ $res=mysql_query("select * from categories where id=".$_GET['id']);
$row=mysql_fetch_array($res,MYSQL_BOTH);
$show="Edit";
}
else
$show="Add";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Add Category</title>
<style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
.style1 {
    color: #004080;
    font-weight: bold;
}
-->
</style>
<script language="javascript">

function check()

{

var d=document.form1;

if(trimstr(d.category.value)==""){alert("Enter category name");d.category.focus();return false;}

}

function trimstr(str) {

str= this != window? this : str;

return str.replace(/^\s+/g, '').replace(/\s+$/g, '');

}

</script>

<link href="bungs.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="1">
  <tr>
    <td colspan="3"><? include"top.php";?></td>
  </tr>
  <tr>
    <td colspan="3" bgcolor="#0000FF"></td>
  </tr>
  <tr>
    <td width="18%" valign="top"><? include"left.php";?></td>
    <td width="1%" bgcolor="#0000FF"></td>
    <td width="81%">
    <table width="100%" border="0" >
  <tr >
    <td align="center" valign="top"><span class="style171"><b><?=$show?> Category</b></span>
    <form name="form1" action="" method="post" onSubmit="return check()">
    <table width="43%" height="143" border="1" align="center" bordercolor="#004080" >
      <tr>
        <td height="137" ><table width="100%"  border="0" align="center">
          <tr>
            <td ><table width="92%"  border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="114"  align="left"><? if(isset($dup_msg)) {echo $dup_msg; }?></td>

              </tr>
              <tr>

                <td width="114">&nbsp;</td>
              </tr>
              <tr>
                <td align="right"><span class="style1">Category</span></td>
                <td width="7" align="center"><strong>:</strong></td>
                <td width="114"><input name="catname" type="text" id="catname"<? if(!empty($_GET['id'])) { ?>value="<?=$row['catname']?>" <? } else  { ?> value="" <? } ?>></td>
              </tr>
              <tr>

                <td valign="top">&nbsp;</td>
              </tr>
              <tr>
                <td height="37">&nbsp;</td>
                <td>&nbsp;</td>
                <td><input type="submit" name="Submit" value="Submit"></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table>
    </form>

    </td>
  </tr>
</table>

    </td>
  </tr>
  <tr>
    <td colspan="3" bgcolor="#0000FF"></td>
  </tr>
  <tr>
    <td colspan="3"><? include"footer.php";?></td>
  </tr>
</table>
</body>
</html>

Dani AI

Generated

The runtime message you saw means a query failed and returned false, so the fetch call had the wrong input. That can come from a bad table name, a syntax error in the SQL, a missing/failed database connection, or permission issues. ’s table-name hint and ’ suggestion to show the DB error are both on point — narrow the failure down by turning on errors and checking the query/connection before trying to fetch rows.

Turn on full error reporting temporarily:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Use a query/connection check that prints the database error and the SQL text (don’t leave this on in production). Example using mysqli (safer and maintained):

$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_errno) {
  die('Connect error: ' . $mysqli->connect_error);
}

if (!$result = $mysqli->query($sql)) {
  die('SQL error: ' . $mysqli->error . "\nSQL: " . $sql);
}

$row = $result->fetch_assoc();

Checklist of targeted items to verify (quick wins):

  • Confirm the actual table name and column names in the database. A one-letter mismatch breaks the query.
  • Verify conn.php actually selects the intended database and that the connection variable used in queries matches the connection you opened.
  • Ensure form input names match the PHP keys you read from $_POST and sanitize/cast IDs (use intval for numeric IDs).
  • Avoid short PHP open tags for portability; prefer <?php.
  • Move away from the deprecated mysql_* extension to mysqli or PDO and use prepared statements to prevent SQL injection.

If the query and connection checks still show no error, paste the failing SQL and the exact mysqli/mysql error (not the whole page source). The error text plus the SQL will pinpoint whether it’s a naming/permission/syntax problem. For mysqli usage and connection examples see the PHP manual: mysqli quickstart.

Recommended Answers

All 9 Replies

Hello friends,
Iam Viru, and iam new to the PHP
i want to add list of categories in that,
but iam unable to add those,
but iam not getting any error message also
can any one help me please.

Thank u in Advance

Hi Viru..
can u send me ur code....(from where u getting categories..)
i will try it..

Thanks for reply Ms.Saritha
i sended the code just now please

Virspy

try echo your insert query.....

Thanks Ms.Shanti

Iam getting but we will write some ting in conn file to dispaly error msg na

viru

MS.Shanti
iam Getting this type of error
can u please

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\www\testing\addcat.php on line 13

Viruspy

check your table name once...
you mentioned category at line 13 and categories at rest of the code..
please check it once and try...

i changed that iam getting same error please

change this line:
$sres=mysql_query($sqry);

to be like this:
$sres=mysql_query($sqry) or die(mysql_error());

this will tell you what the problem is.

if your thread got solved...
make it solved ....always...

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.