Jump to content


Not inserting into database


2 replies to this topic

#1 zetsumei

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 269 posts
  • Gender:Male
  • Location:127.0.0.1

Posted 23 January 2007 - 09:01 PM

I'm trying to get a tutorial system to work, and I get no errors what so ever, but it doesn't insert into the database. I go to the admin page to add a category, but it doesn't add it to the database.

heres the functions.php

<?php

function list_categories() {
$query = mysql_query("SELECT * FROM tutorials_categories");
while($row = mysql_fetch_array($query)) {
$query2 = mysql_query("SELECT * FROM tutorials WHERE cat_id = '$row[id]'");
if(mysql_num_rows($query2) == 0) {
echo" $row[catname] |";
}
else {
echo" <a href='$self?action=cat&id=$row[id]'>$row[catname]</a> |";
}
}
}

function list_unval_tutorials() {
echo"<strong>Un-Validated tutorials</strong>";
$query = mysql_query("SELECT * FROM tutorials WHERE valid = '0' ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
echo"
<table width='100%'>
  <tr>
	<td width='60%'>
	$row[tutname]
	</td>
	<td width='10%'>
	  <a href='$self?action=mod&id=$row[id]'>Modify</a>
	</td>
	<td width='10%'>
	  <a href='$self?action=delete&id=$row[id]'>Delete</a>
	</td>
	<td width='10%'>
	  <a href='$self?action=view&id=$row[id]'>View</a>
	</td>
	<td width='10%'>
	  <a href='$self?action=validate&id=$row[id]'>Validate?</a>
	</td>
  </tr>
</table>
";
}
}

function list_val_tutorials() {
echo"<strong>Validated tutorials</strong>";
$query = mysql_query("SELECT * FROM tutorials WHERE valid = '1' ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
echo"
<table width='100%'>
  <tr>
	<td width='60%'>
	$row[tutname]
	</td>
	<td width='10%'>
	  <a href='$self?action=mod&id=$row[id]'>Modify</a>
	</td>
	<td width='10%'>
	  <a href='$self?action=delete&id=$row[id]'>Delete</a>
	</td>
	<td width='10%'>
	  <a href='$self?action=view&id=$row[id]'>View</a>
	</td>
  </tr>
</table>
";
}
echo"";
}

function del_tutorial() {
if(!isset($_POST['delete'])) {
if($_GET['id']) {
$id = (int)$_GET['id'];
$query = "DELETE FROM tutorials WHERE id = '$id' LIMIT 1";
mysql_query($query);
echo"Tutorial Deleted";

}
}
}

function val_tutorial() {
if(!isset($_POST['validate'])) {
if($_GET['id']) {
$id = (int)$_GET['id'];
$query = "UPDATE tutorials SET valid = '1' WHERE id = '$id' LIMIT 1";
mysql_query($query);
echo"Tutorial Validated";

}
}
}

function view_tutorial() {
if(!isset($_POST['view'])) {
if($_GET['id']) {
$id = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM tutorials WHERE id = '$id' LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo"You are viewing: <b>$row[tutname]</b> 
	 $row[tuttext] 
	 Some other info: 
	 Author: $row[authname] | Date posted: $row[date] |
	 ";
}
}
}
}

function add_tut() {
if(!isset($_POST['add_tut'])) {
echo"
<form action='$self?action=add_tut' name='addtutorials' method='post'>
<table>
	<tr>
		<td>
		Tutorial name:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='tutname'>
		</td>
	</tr>
		<td>
		Author name:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='authname'>
		</td>
	</tr>
	<tr>
		<td>
		Categories:
		</td>
	</tr>
	<tr>
		<td>
		<select name='category'>";
		$query = mysql_query("SELECT * FROM tutorials_categories ORDER 

BY id ASC");
		while($row = mysql_fetch_array($query)) {
		echo"<option value='$row[id]'>$row[catname]";
		}
		echo"
		</select>
		</td>
	</tr>
	<tr>
		<td>	
		Image:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='image'> (40x40px)
		</td>
	</tr>
	<tr>
		<td>
		Email:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='email'>
		</td>
	</tr>
	<tr>
		<td>
		Tutorial Text:
		</td>
	</tr>
	<tr>
		<td>
		<textarea cols='50' rows='20' name='tuttext'></textarea><br 

/>
		</td>
	</tr>
	<tr>
		<td>
		Short description:
		</td>
	</tr>
	<tr>
		<td>
		<textarea cols='50' rows='5' name='shortdes'></textarea><br 

/>
		

		</td>
	</tr>
	<tr>
		<td>
		<input type='submit' name='add_tutorials' value='submit 

tutorial'>
		</td>
	</tr>
</table>
</form>";   
}
elseif(isset($_POST['add_tutorials'])) 
{

$tutname = mysql_real_escape_string(strip_tags($_POST['tutname']));
$authname = mysql_real_escape_string(strip_tags($_POST['authname']));
$image = mysql_real_escape_string(strip_tags($_POST['image']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$category = mysql_real_escape_string(strip_tags($_POST['category']));
$tuttext = $_POST['tuttext'];
$shortdes = $_POST['shortdes'];
$date = date("d.m.y");

$error = array();

if(empty($tutname)) {
$error[] = "Please enter your tutorial name.";
}
if(empty($authname)) {
$error[] = "Please enter your (the author's) name.";
}
if(empty($image)) {
$error[] = "Please enter an image.";
}
if(empty($tuttext)) {
$error[] = "Hello. You seem to have entered nothing into the tutorial text 

area.";
}
if(empty($shortdes)) {
$error[] = "Enter a short description."; 
}

if(count($error)>0) {
echo"<font size='3' color='#CC0000'>ERROR:</font>";
foreach($error as $error2)
echo"$error2";
}

else {
$mysql = "INSERT INTO tutorials(tutname, email, authname, image, tuttext, shortdes, valid, cat_id, date, views) VALUES('$tutname', '$email', '$authname', '$image', '$tuttext', '$shortdes', '0', '$category', '$date', '0')";

mysql_query($mysql);

echo"Your tutorial has successfully been submitted for valadation.";

}
}
}

function mod_tut() {
if(!isset($_POST['modify'])) {
if($_GET['id']) {
$id = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM tutorials WHERE id = '$id' LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo"
<form name='modify' action='$self?action=mod&id=$row[id]' method='post'>
<table>
	<tr>
		<td>
		Tutorial name:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='modtutname' value='$row[tutname]'>
		</td>
	</tr>
		<td>
		Author name:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='modauthname' value='$row[authname]'>
		</td>
	</tr>
	<tr>
		<td>	
		Image:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='modimage' value='$row[image]'>(40x40px) 
		</td>
	</tr>
	<tr>
		<td>
		Email:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='modemail' value='$row[email]'>
		</td>
	</tr>

	<tr>
		<td>
		Tutorial Text:
		</td>
	</tr>
	<tr>
		<td>
		<textarea cols='50' rows='20' name='modtuttext'>$row[tuttext]</textarea>
		</td>
	</tr>
	<tr>
		<td>
		Short description:
		</td>
	</tr>
	<tr>
		<td>
		<textarea cols='50' rows='5' name='modshortdes'>$row[shortdes]</textarea>
		

		</td>
	</tr>
	<tr>
		<td>
		<input type='submit' name='modify' value='modify'>
		</td>
	</tr>
</table>
</form>";	
}
}
}

elseif(isset($_POST['modify'])) 
{

$modtutname = mysql_real_escape_string(strip_tags($_POST['modtutname']));
$modauthname = mysql_real_escape_string(strip_tags($_POST['modauthname']));
$modimage = mysql_real_escape_string(strip_tags($_POST['modimage']));
$modemail = mysql_real_escape_string(strip_tags($_POST['modemail']));
$modcategory = mysql_real_escape_string(strip_tags($_POST['modcategory']));
$modtuttext = $_POST['modtuttext'];
$modshortdes = $_POST['modshortdes'];

$err3 = array();

if(empty($modtutname)) {
$error[] = "Please enter your tutorial name.";
}
if(empty($modauthname)) {
$error[] = "Please enter your (the author's) name.";
}
if(empty($modimage)) {
$error[] = "Please enter an image.";
}
if(empty($modtuttext)) {
$error[] = "Hello. You seem to have entered nothing into the tutorial text 

area.";
}
if(empty($modshortdes)) {
$error[] = "Enter a short description."; 
}


if(count($err3)>0) {
echo"ERROR:";
foreach($err3 as $err5) 
echo"$err5";
}

else {
if($_GET['id']) {
$id = (int)$_GET['id'];
$sql1 = "UPDATE tutorials SET tutname = '$modtutname' WHERE id = '$id'";
$sql2 = "UPDATE tutorials SET authname = '$modauthname' WHERE id = '$id'";
$sql3 = "UPDATE tutorials SET image = '$modimage ' WHERE id = '$id'";
$sql4 = "UPDATE tutorials SET email = '$modemail ' WHERE id = '$id'";
$sql5 = "UPDATE tutorials SET category = '$modcategory' WHERE id = '$id'";
$sql6 = "UPDATE tutorials SET tuttext = '$modtuttext' WHERE id = '$id'";
$sql7 = "UPDATE tutorials SET shortdes = '$modshortdes ' WHERE id = '$id'";

mysql_query($sql1);
mysql_query($sql2);
mysql_query($sql3);
mysql_query($sql4);
mysql_query($sql5);
mysql_query($sql6);
mysql_query($sql7);

echo"Tutorial successfully updated.";
}
}
}
}

function add_cat() {
if(!isset($_POST['add_cat'])) {
echo"
<form action='$self?action=add_cat' name='addcategories' method='post'>
<table>
	<tr>
		<td>
		Tutorial name:
		</td>
	</tr>
	<tr>
		<td>
		<input type='text' name='catname'>
		</td>
	</tr>
	<tr>
		<td>
		<input type='submit' name='add_categories' value='submit category'>
		</td>
	</tr>
</table>
</form>";   
}
elseif(isset($_POST['add_categories'])) 
{

$catname = mysql_real_escape_string(strip_tags($_POST['catname']));

$error = array();

if(empty($catname)) {
$error[] = "Please enter your tutorial name.";
}

if(count($error)>0) {
echo"<font size='3' color='#CC0000'>ERROR:</font>";
foreach($error as $error2)
echo"$error2";
}

else {
$mysql = "INSERT INTO tutorials_categories('catname') VALUES('$catname')";

mysql_query($mysql);

echo"Your category has successfully been submitted.";

}
}
}

?>

It's the add_cat function, I put the whole code incase there was something else wrong.

Edited by zetsumei, 23 January 2007 - 09:01 PM.


#2 LkuHomer

    Young Padawan

  • Members
  • Pip
  • 173 posts
  • Gender:Male
  • Location:Dela'where?

Posted 23 January 2007 - 10:59 PM

It might be that there is a lot of code there and I'm tired, but it looks like your not connecting to the db anywhere.

Edited by LkuHomer, 23 January 2007 - 11:01 PM.


#3 zetsumei

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 269 posts
  • Gender:Male
  • Location:127.0.0.1

Posted 23 January 2007 - 11:35 PM

ooo, im stupid...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users