11 Replies - 624 Views - Last Post: 10 February 2011 - 03:50 PM Rate Topic: -----

#1 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

not saving into my database & website.

Posted 08 February 2011 - 10:55 PM

<?php session_start(); ?>

<?php
 function showForm($name,$pnum,$pic,$error)
 {
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="style2.css" />
	<title>Phonebook</title>
</head>

<body>
<center>
<table class="maintable">
<tr>
	<td>
<div class="innerbg">
<BR />
<h1 align="center">Phonebook</h1>
<BR />
</div>
<hr style="border:solid 1px #0FF;" />
<hr  style="border:solid 1px #0FF;"/>
</table>
<br />

<table class="innerbg">
<tr>
<td>
<form action="" method="post" enctype="multipart/form-date">

 <div>
 <table>

 	<tr>
    	<td>
<strong>Name: </strong><td> <input type="text" name="name" value="<?php echo $name; ?>" />
 <tr><td>
<strong>Number: </strong><td> <input type="text" name="pnum" value="<?php echo $pnum; ?>" />
 <tr><td>
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Picture:
<td><br/><br/> <input name="pic" type="file" value="<?php echo $pic; ?>" /><br>
<br/>
  <input type="submit" name="submit" value="Submit">
  <a href="index.php"><input type="button" value="Back to Home pg."></a>
 </div>
 </form>
 </table>
  <div class="infobox" style="position:fixed;top:200px;left:60px;width:180px;">
 <?php 
 if ($error != '')
 	{
 		echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 	}
 ?> 
  
 </table>
 <br />
 </center>
 </body>
 </html>
 
 <?php 
 }
 include('connectdb.php');

		 if (isset($_POST['submit']))
			{ 
				 $pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));
				 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
				 $pnum = mysql_real_escape_string(htmlspecialchars($_POST['pnum']));
				 
				 
				 
					$uploaddir = 'ufiles/';
					$uploadfile = $uploaddir . basename($_FILES['pic']['name']);
  
				echo "<p>";
				    move_uploaded_file($_FILES['pic']['tmp_name'], $uploadfile);
				
					if ($name == '' || $pnum == '' || $pic =='')
							{

						// $error = 'ERROR: Please fill in all required fields!';
						 showForm($name, $pnum, $pic,$error);
					}
				else
					{
					 mysql_query("INSERT phonebook SET pic='$pic',name='$name',pnum='$pnum'")
					 or die(mysql_error());
					
					echo '<br><br><br><br><br><br><br><center>The Record has been successfully added</center>';
					echo '<center><a href="index.php">Go back to Home page</a></center>';
					}
			
			
			}
		 else  
			 {
			 
				showForm('','','','');
			 }
			 
?> 
<link rel="stylesheet" href="style2.css" />
	



My problem is after adding a new Record
it contains
Name:
Number:
Picture:

i successfully saved the image to my folder but the funny thing is
the name and the number + the picture 'location' is not saved to my Database and to my website View Record page or the Edit and so on where u can see the records that has been entered.

Is This A Good Question/Topic? 0
  • +

Replies To: not saving into my database & website.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: not saving into my database & website.

Posted 09 February 2011 - 04:52 AM

You're mixing up your SQL syntax. It's INSERT INTO table (pic, name, num) VALUES ('$pic', '$name', '$num');.

Good on you for using mysql_real_escape_string()!
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: not saving into my database & website.

Posted 09 February 2011 - 08:52 AM

Seriously work on properly indenting your code so others can read it! Also, always separate your HTML and PHP. You should not have in-line PHP in your HTML. Read in your HTML from a template file, modify it accordingly, then output it.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: not saving into my database & website.

Posted 09 February 2011 - 01:13 PM

I want to apologize for being wrong on my criticism of your use of INSERT. I didn't realize that INSERT...SET col=value was a valid variation on that syntax.
Was This Post Helpful? 2
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,803
  • Joined: 08-August 08

Re: not saving into my database & website.

Posted 09 February 2011 - 02:10 PM

See this for some ideas on code separation:
http://www.dreaminco...ode-separation/
Little things like this:
<?php session_start(); ?>

<?php


make your code longer and harder to read.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: not saving into my database & website.

Posted 09 February 2011 - 02:54 PM

Print the value of your $_POST array with print_r and see if the values are getting set properly on entering the script.
Was This Post Helpful? 1
  • +
  • -

#7 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: not saving into my database & website.

Posted 10 February 2011 - 03:14 AM

@macox: sorry for having a bad alignment of my codes xD and thanks for that , maybe some other time i'll try to fix it :D

@JackOfallTrades: its ok lol and thanks for the help
@CT: thanks


All help are appreciated and thanks

But still trying to debug it if what went wrong .-.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,803
  • Joined: 08-August 08

Re: not saving into my database & website.

Posted 10 February 2011 - 05:07 AM

View PostSyfer, on 10 February 2011 - 06:14 AM, said:

But still trying to debug it if what went wrong .-.

Here's why that's so difficult: imagine that you have some code:

<?php
for($i = 0; $i < 10; $i++)
{
   // do something
   // do something
   // do something
   // do something
} 

for($j= 0; $j < 50; $i++)
{
   // do something
   // do something
   // do something
   // do something
} 

if($result_from_above == $other_result)
{
  // do something
}

echo $output;
?>


Now imagine that you take this code and insert random text, which is exactly what HTML looks like to PHP:

<?php
for($i = 0; $i < 10; $i++)
{
   // do something
   // do something
?> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam quam quam, eleifend dictum aliquam mollis, facilisis id sapien. Maecenas tempor sodales leo interdum pharetra. Pellentesque sit amet lacus eget velit rhoncus ullamcorper. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nullam iaculis mollis sagittis. Praesent accumsan lacinia nunc. Aenean a mi eros. Curabitur accumsan feugiat augue, et consectetur libero tincidunt sed. Aenean semper egestas neque, at sollicitudin sapien mollis a. Sed aliquam, arcu quis accumsan imperdiet, velit turpis blandit purus, vitae facilisis eros risus ac neque. Vestibulum nisi dolor, tempor at aliquet ac, accumsan id nulla<?php
   // do something
   // do something
} 
?> Phasellus tempor, mauris vitae convallis accumsan, lectus augue scelerisque quam, in sollicitudin enim nisi sit amet risus. Sed in mi a lacus malesuada fermentum. Sed pretium vehicula felis, nec pharetra augue vehicula et. Nam ligula metus, suscipit pulvinar pellentesque nec, pharetra in lorem. Vestibulum dictum tortor et ipsum venenatis fermentum. Ut ac sem neque. Duis id libero nunc, et pretium augue. Donec neque urna, mattis id vehicula eget, interdum non diam. Aliquam id risus libero. Ut eget nibh id lorem ullamcorper venenatis vel nec nunc. Duis venenatis tincidunt augue eu fringilla. Donec dictum, nisl laoreet sagittis porta, quam enim placerat magna, tempor pharetra turpis elit et orci. Morbi consequat tortor nec purus dapibus ut faucibus risus faucibus. Donec elit sapien, malesuada tincidunt egestas quis, euismod ac enim. Duis interdum venenatis sem, a laoreet eros mattis sed. Fusce dapibus faucibus orci eget dapibus. Suspendisse tempus vestibulum ante non vehicula. Proin rhoncus purus mattis nibh eleifend tristique.<?php
for($j= 0; $j < 50; $i++)
{
   // do something
   // do something
   // do something
   // do something
} 
?> Sed ut rhoncus est. Mauris vitae odio ligula, quis faucibus nulla. Nunc eu scelerisque arcu. Nunc facilisis augue sed turpis elementum gravida. Pellentesque pulvinar nunc ac odio ornare eu sodales purus convallis. Nam eros neque, ultricies cursus imperdiet eget, gravida eu nisi. Praesent venenatis tincidunt enim, ut sagittis odio pellentesque sed. Ut id orci in mi accumsan feugiat at sit amet sem. Morbi vestibulum nunc neque. Nulla sit amet est id erat pulvinar viverra. In laoreet molestie <?php
if($result_from_above == $other_result)
{
  ?> Phasellus euismod varius velit, ac semper erat ultricies at. Nullam accumsan augue a quam molestie eu congue nisi dictum. Phasellus sit amet risus tortor, eget malesuada ante. Nullam convallis odio vel nibh pharetra fermentum. Proin ac magna sit amet tellus aliquet dapibus ut ut eros. In mattis pharetra varius. Curabitur semper, ipsum sed elementum porta, turpis leo mollis arcu, id consequat lorem nibh ut enim. Sed velit ante, tempus in blandit a, molestie in nulla. Etiam pellentesque laoreet lacus at accumsan. Ut at hendrerit diam. Nullam sagittis elementum erat non consectetur. Duis ut dui lorem. Quisque sed ante ante. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Maecenas ligula tortor, suscipit vel scelerisque et, ullamcorper vitae leo. Donec ac ante ligula. Morbi congue semper sollicitudin. Curabitur posuere tortor in libero vestibulum non viverra tellus facilisis. Suspendisse ornare leo et est rutrum nec ornare neque bibendum. Proin vestibulum leo a mi molestie cursus. <?php// do something
}

echo $output;
?>

Which do you think is easier to debug?
Was This Post Helpful? 0
  • +
  • -

#9 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: not saving into my database & website.

Posted 10 February 2011 - 07:46 AM

I already fixed it and the last part i need to fix is my Edit part lol.
check the output.

Code for my Edit.php
<?php
 function showForm($id,$name, $pnum, $pic,$rating)
 {
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="style2.css" title="Welcome to Baguio City" media="screen,projection" />
	<title>Phonebook</title>
</head>

<body>
<center>
<table class="maintable">
<tr>
	<td>
<div class="innerbg">
<BR />
<h1 align="center">Phonebook</h1>
<BR />
</div>
<div class="innerbg">
<div style="margin-left:15px;">
 <?php 
 if ($error != '')
 	{
 		echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 	}
 ?> 
 
 <form action="" method="post" enctype="multipart/form-date">
 <input type="hidden" name="id" value="<?php echo $id; ddd?>"/>
 <div>
 <p><strong>ID: </strong> <?php echo $id; ?></p>
 <strong>Name:</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
 <strong>Phone Number:</strong><input type="text" name="pnum" value="<?php echo $pnum; ?>"/><br/>
 <br>
<strong>Picture:</strong><input type="file" name="pic" value="<img width="70" height="*" src="ufiles/<?php echo $pict;?>/>"/>
                            " />
 <br>
 <input type="submit" name="submit" value="Submit">
 <a href="index.php"><input type="submit" name="submit" value="Go back to home page"></a>
 </div>
 </form> 
 </body>
 </html> 
 <?php
 }




 include('connectdb.php');
 

 if (isset($_POST['submit']))
 	{ 
	
		$uploaddir = 'ufiles/';
		$uploadfile = $uploaddir . basename($_FILES['pic']['name']);
		move_uploaded_file($_FILES['pic']['tmp_name'], $uploadfile);
 
 		if (is_numeric($_POST['id']))
 			{

				 $id = $_POST['id'];
				 $pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));
				 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
				 $pnum = mysql_real_escape_string(htmlspecialchars($_POST['pnum']));
				 
			
				 
 
 				if ($name== '')
 					{
						
						
						 
						
						 showForm($id, $name, $pnum,$pic ,$error);
					 }
 				else
					 {
						
						 mysql_query("UPDATE phonebook SET name='$name', pnum='$pnum' ,pic='$pic' WHERE id='$id'")
						 or die(mysql_error()); 
						 
						 
						 header("Location: paginatededit.php"); 
					 }
 			}
 		else
 			{
			
			 echo 'Error!';
			}
 	}
 else
	
	 {
	 
	
 		if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 			{
			
			 $id = $_GET['id'];
			 $result = mysql_query("SELECT * FROM phonebook WHERE id='$id'")
			 or die(mysql_error()); 
			 $row = mysql_fetch_array($result);
			
			 if($row)
			 	{
 
					  $pic = $row['pic'];
					 $name = $row['name'];
					 $pnum = $row['pnum'];
					
					 showForm($id,$pic,$name, $pnum,'');
				 }
 			else
 					
				 {
				 	echo "No results!";
				 }
 		}
 	else
	 	
	 	{
	 		header("Location: paginatededit.php");
		}
 }
?>



i do really really need help in this part...

in editing the record
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: not saving into my database & website.

Posted 10 February 2011 - 07:49 AM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#11 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: not saving into my database & website.

Posted 10 February 2011 - 07:59 AM

View Postmacosxnerd101, on 10 February 2011 - 07:49 AM, said:

What specific problems or errors are you encountering?


here's the output xD

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: not saving into my database & website.

Posted 10 February 2011 - 03:50 PM

The error i encounter is everytime i want to edit my record
the location of my image as and the name are popping up at my phone number text box and sometimes the location of the image goes to my name textbox
and the name goes to phone number which is not right and since its edit it shouldn't be like that

and my database or website won't update after editing a certain record.

This post has been edited by Syfer: 10 February 2011 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1