Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am new PHP learner and I've attempted several times to insert my codes to table.I think there is mistake in my php codes.But I wasn't able to solve it myself

What I have tried:

PHP
<pre><?php
include "config.php";
include "db.php";

$db = new database();


$query = "SELECT * FROM categories";
$cats = $db ->select($query);

if(isset($_POST['submit'])){
	$title = $_POST['title'];
	$content = $_POST['content'];
	$cat = $_POST['cat'];
	$image = $_FILES['image'] ['name'];
	$image_tmp = $_FILES['image'] ['tmp_name'];
	move_uploaded_file($img_tmp,"../images/$image");


	$query = "INSERT INTO posts (category_id,title,content,image) VALUES ('$cat',$title,'$content','$image')";
     $run = $db->insert($query);
}

     ?>




<!DOCTYPE html>
<html>
<head>
	<title>Insert Post</title>
</head>
<body>

<form action="insert.php" method="post" enctype="multipart/form-data">
<center><table width="800" align="center" border="2"></center>
<tr bgcolor="orange">
<td colspan="6"><h1 style="text-align:center">Insert new Post:</h1></td>
</tr>

<tr>
	<td align="right" bgcolor="orange">Post title</td>
	<td><input type="text" name="title" size="60"></td>
</tr>
<tr>
	<td align="right" bgcolor="orange">Post Category</td>
	<td>

		<select name="cat">
		 <option >Select a Category</option>
		 <?php while($row1 = $cats->fetch_array()) :?>
		 	<option value="<?php echo $row['id'];?>"><?php echo $row1 ['title'];?></option>
		<?php endwhile ;?>
		</select>
	</td>

</tr>



<tr>
	<td align="right" bgcolor="orange">Post image</td>
	<td><input type="file" name="image" size="50"></td>
</tr>
<tr>
	<td align="right" bgcolor="orange">Post Content</td>
	<td><textarea name="content" rows="15" cols="40"></textarea></td>
</tr>
<tr>
	
	<td colspan="6" align="center" bgcolor="orange"><input type="Submit" name="Submit" value="Publish now"/></td>
</tr>

</body>
</html>
Posted
Updated 26-Oct-17 1:12am

Have not done PHP in years but should you have single quotes around $title ?
 
Share this answer
 
Comments
ALEX8998 26-Oct-17 6:55am    
done! but no changes yet
Your test of
if(isset($_POST['submit']))
will always pass in that an input control of type 'submit' should not have (or be tested) for a value - it's job is to send the contents of the form to the target of action="inset.php". Since you hard-coded a value it will always have one.

Also, not knowing what is in "db.php" or "config.php", I don't know if you're connecting to a database with $db= new database(); Do you even have a connection parameters? Can you retrieve data from the table so you at least know you have a connection?

If you simply cut/pasted these from some online source then there's essentially no chance they'll work for you since your setup will have different locations, credentials, and everything else. (without understanding, cut/paste = waste)

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900