/    Sign up×
Community /Pin to ProfileBookmark

Multipule Table Insert

I’m having some problems updating a mySQL table with my PHP script. I am trying to design a page where you can create a category (for clothing) and insert it into table called categories. As there will be subcategories I have created a table, called subcategories, which will contain the parent_id and child_id.

No matter what I do I can’t insert, update or delete to the new table. Any ideas? I’ve included my code.

Thanks!
Neil

<?php
include(‘../../include/site_config.php’);
include(‘../../include/admin_config.php’);
checklogin(‘categories’);

@debug(“in types module, auth ok”);

$delete = $_GET[‘delete’];
$category = $_
GET[‘category’];
$edit_id = $_GET[‘edit_id’];
$edit = $_
GET[‘edit’];
$new_name1= strtolower(trim(ereg_replace(” “,””,$_GET[‘new_name’])));
$category2= strtolower(trim(ereg_replace(” “,””,$_
GET[‘category’])));
$dir1 = “../../” . $new_name1;
$dir2 = “../../” . $category2;

if(!empty($delete))
{
$sql = “DELETE FROM categories WHERE id=’$delete’ LIMIT 1”;
mysql_query($sql,$cn);
}

if(!empty($new_name))
{
$sql = “UPDATE categories SET category='”.$new_name1.”‘, link='”.$new_name1.”‘ WHERE id='”.$edit_id.”‘”;
mysql_query($sql,$cn);
$old_umask = umask(0);
@mkdir($dir1, 01777);
$file = “./../../” . $new_name1 . “/” . “index.php”;
$fp = fopen (“$file”, “wb”);
$content = ‘<? $category=”‘ . $new_name1 . ‘”; include (“../category.php”) ?>’;
fwrite($fp, $content);
fclose($fp);
}

if(!empty($category))
{
$sql = “INSERT INTO categories (category, link) VALUES(‘”.$category.”‘, ‘”.$category2.”‘)”;
mysql_query($sql,$cn);
$old_umask = umask(0);
@mkdir($dir2, 01777);
$file = “./../../” . $category2 . “/” . “index.php”;
$fp = fopen (“$file”, “wb”);
$content = ‘<? $category=”‘ . $category . ‘”; include (“../category.php”) ?>’;
fwrite($fp, $content);
fclose($fp);
}

$sql = “SELECT * FROM categories ORDER BY category”;
$result = mysql_query($sql,$cn);
?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”>
<html>
<head>
<title>Categories Index</title>
<link rel=’stylesheet’ href=’../admin.css’>
</head>
<body bgcolor=”#FFFFFF” text=”#000000″ link=”#FF0000″ vlink=”#800000″ alink=”#FF00FF”>
<center>

<form action='<?=$PHP_SELF?>’ method=’get’>
<table cellpadding=’3′ cellspacing=’0′>
<tr>
<td class=’mainHeader’ colspan=’2′>Categories Index</td>
</tr>
<tr>
<td class=’formTitleLeft’>Index</td>
<td class=’searchResult’>&nbsp;</td>
</tr>
<?
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<td><?=$row[‘category’]?></td>
<td class=’searchResult’ align=’center’><a href='<?=$PHP_SELF?>?edit=<?=$row[‘id’]?>’><img border=’0′ src=’../images/edit.gif’></a>&nbsp;&nbsp;<a href='<?=$PHP_SELF?>?delete=<?=$row[‘id’]?>’ onClick=’return confirm(“Are you Sure?nThis cannot be undone.”);’><img border=’0′ src=’../images/delete.gif’></a></td>
</tr>
<?
}
?>
<tr>
<td><input type=’text’ name=’category’></td>
<td class=’searchResult’ align=’center’><input type=’submit’ value=’Add Category’></td>
</tr>
<?
if(!empty($edit))
{
$sql = “SELECT * FROM categories WHERE id='”.$edit.”‘”;
$result = mysql_query($sql,$cn);
$row = mysql_fetch_assoc($result);
?>
<tr>
<td colspan=’2′><br /><br /></td>
</tr>
<tr>
<td class=’mainHeader’ colspan=’3′>Edit Category</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<input type=’hidden’ name=’edit_id’ value='<?=$row[‘id’]?>’>
</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<input type=’text’ name=’new_name’ value='<?=$row[‘category’]?>’>
</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<?
$SQL_2 = “SELECT *
FROM categories ORDER BY category ASC”;
$RESULT_2 = MYSQL_QUERY($SQL_2);
?>
<select name=”parent_id3″ onChange=”populate3(myfrm)” width=”400″>
<option value=”0″ selected>- – – – SELECT A CATEGORY – – – -</option>
<?
WHILE($ROW_2 = MYSQL_FETCH_ARRAY($RESULT_2))
{
ECHO ‘<option value=”‘.$ROW_2[‘id’].'”>’.$ROW_2[‘category’].”</option> n”;
}
?>
</select>
</td>
</tr>
<tr>
<td class=’searchResult’ align=’center’ colspan=’2′><input type=’submit’ value=’Update Category’></td>
</tr>
<tr>
<td colspan=’2′ align=’center’>
<a href=”set_image.php?id=<?=$row[‘id’]?>”>Change Image</a>
</td>
</tr>
<?
}
?>
</table>
</form>
</center>
</body>
</html>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 02.2005 — First thing to do is check your return values from mysql_query()'s, and if it fails get the mysql error:
[code=php]
$result = mysql_query($query) or die("SQL ERROR on '$query' - " . mysql_error());
[/code]

Hopefully this will then point you in the right direction to figure out what's wrong.
×

Success!

Help @neilmunn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...