I have a Simple CMS i made but i cant anderstand how to connect the content to the category
i need another mysql table for cat?
how can i add this in to the form?
which functions will show the cat in the Add content page?

Member Avatar for diafol

Show your exisitng code, your description is unclear.

for example -
the form

 <form action="add.php" method="post">
 title: <input type="text" name="title">
 discription: <input type="text" name="discription">
 content: <input type="content" name="content">
 <input type="submit">
 </form>

i want to add to the form a global categories

the add.php

<?php
require 'config.php';

// Get values from form 
 $title=$_POST['title'];
 $description=$_POST['description'];
 $content=$_POST['content'];

 // Insert data into mysql 
 $sql="INSERT INTO $content (titel, discription, content )VALUES('$title', '$discription', '$content')";
 $result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='addp.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
mysql_close($con);
 ?> 
Member Avatar for diafol

Ok, I think I know what you want. I'd do this...

1) Create a new categories table in MySQL: cat_id [PK, int], category [varchar]
2) Run a SELECT query on a category table and extract both fields. In the while loop, you can build the select dropdown:

$select = '';
while($d = mysql_fetch_assoc($result)){
   $select .= "<option value='{$d['cat_id']}'>{$d['category']}</option>";
}

3) Insert the variable in the required place, e.g.

<select name="cat">
<?php echo $select;?>
</select>

4) You pick up the category id:

$cat_id=$_POST['cat'];

5) By now you should have realised that you need a cat_id field in your content table.

6) INSERT SQL after you clean all the post variables with mysql_real_escape_string()

INSERT INTO content (title, description, cat_id )VALUES('$title', '$description', $cat_id)

** You've spelled some fields incorrectly

OK, that's my 2p. Note that you just have the ONE content table, with each record linked (related) to a category

there is an error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/content/60/10533160/html/cms/in.php on line 3
Member Avatar for diafol

means you didn't run a query before the mysql_fetch_assoc

still not working

<?php

require 'config.php';



$select = 'mysqli_query("SELECT * FROM categories")';
while($d = mysql_fetch_assoc($result)){
   $select .= "<option value='{$d['cat_id']}'>{$d['category']}</option>";
}
?>
<html>
 <body>

 <form action="main.php" method="post">
 Firstname: <input type="text" name="title">
 Lastname: <input type="text" name="discription">
 Age: <input type="content" name="content">
<select name="cat_id">
<?php echo $select;?>
</select>


 <input type="submit">
 </form>

 </body>
 </html> 

Line 7 $select = 'mysqli_query("SELECT * FROM categories")'; Why do you have the mysqli_query function itself in quotes?

So what is the correct way to write the query

You have quotes surrounding the entire function, so it's a string, simply remove the quotes so it reads: $select = mysqli_query("SELECT * FROM categories");
That should fix your problem.

not working

<?php





require 'config.php';



 $select = mysqli_query("SELECT * FROM categories");
while($d = mysql_fetch_assoc($result)){
   $select .= "<option value='{$d['cat_id']}'>{$d['category']}</option>";
}
?>

ERROR

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/content/60/10533160/html/cms/in.php on line 7

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/content/60/10533160/html/cms/in.php on line 8

<?php

$query = "SELECT * FROM categories";
$result = mysqli_query($query, $dbc);
while ($row = mysqli_fetch_assoc($result)) {
    // Do stuff
}

?>
Member Avatar for diafol

As tintin shows, you need to include the connection ($dbc). I'm assuming this is set in your config.php file. Of course it may be called something other than $dbc. Just change the name of the variable in that case.

still give the same error
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/content/60/10533160/html/cms/in.php on line 6

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/content/60/10533160/html/cms/in.php on line 7

line 6

$result = mysqli_query($query, $con);

line 7

while ($row = mysqli_fetch_assoc($result)) {
Member Avatar for diafol

I'm really sorry mat - my mistake, the parameter order in Tinnin's post is wrong - yours should be:

$result = mysqli_query($con, $query);

Anyway, you can check this out yourself at http://php.net/manual/en/mysqli.query.php and look at the 'Procedural Style' not the 'OOP Style'. However, I would suggest maybe moving towards OOP if your site needs it.

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.