update my JOIN table - php

I have built a system where I can create a category and a document. the categories live in the cat_list table and the documents live in the doc_list table. there is a column in the doc_list table called cat_no which takes an array or the categories that belong to that doc.
newfile.php
<?php
require_once '../../db_con.php';
try{
// Selecting entire row from cat_list table
$results = $dbh->query("SELECT * FROM cat_list");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$cat = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<form action="actions/newDocAdd.php" method="post" id="rtf" name="">
<input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />
<?php
foreach($cat as $cat){
echo
'<input type="checkbox" value="" name=""> ' .$cat["cat_title"]. '</a><br>';
}
?>
<br><br>
<textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
<iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>
<br><br>
<input onclick="formsubmit()" type="submit" value="Create Document" name="submit"/>
</form>
I have cut alot out of the form because there is alot of JS because it is a WYSIWYG creator hence the iframe. But the key area is where I lsit out categories above as checkboxes, I need to allow for that to then post a number (or array of numbers if more than one is clicked) into the col_no column in the doc)list table.
Here is the script which posts the data:
<?
session_start();
session_regenerate_id();
if(!isset($_SESSION['user'])){
header("Location: ../index.php");
exit;
}
if(isset($_POST["submit"])){
include_once'../../config.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dashboardr",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO doc_list (doc_title, doc_content, doc_created, user_id) VALUES ('".$_POST["doc_title"]."','".$_POST["doc_content"]."', NOW(), '".$_SESSION['user']."''".$_POST['cat_no']."' )";
print_r($_POST);
if ($dbh->query($sql)) {
header ('Location: ../docList.php?success=1');
}
else{
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
UPDATE
<?php
foreach($cat as $cat){
echo
'<input type="checkbox" value="$cat["cat_id"]" name="cat_no"> ' .$cat["cat_title"]. '</a><br>';
}
?>
So I can get it to post in a value of "0" but I need it to be an array of the ID's of which I am posting, what is it I am doing wrong here?

Related

How to store and get two values in PHP?

This is process_upcategory.php
I want to update the category name or the category id with another category name/id by its category id or by its category name.
I'm new to php
<?php
require('includes/config.php');
if(!empty($_POST))
{
$msg=array();
if(empty($_POST['cat']))
{
$msg[]="Please full fill all requirement";
}
if(!empty($msg))
{
echo '<b>Error:-</b><br>';
foreach($msg as $k)
{
echo '<li>'.$k;
}
}
else
{
$cat_nm=$_POST['cat[0]'];
$cat_id=$_POST['cat[1]'];
$query= "UPDATE `category` SET cat_nm='$cat_nm' WHERE cat_id='$cat_id'";
mysqli_query($conn,$query) or die("can't Execute...");
mysql_close($link);
header("location:category.php");
}
}
else
{
header("location:index.php");
}
?>
Now this is category.php, just a snippet of code. Not whole code
<form action='process_upcategory.php' method='POST'>
<b style="color:darkgreen">UPDATE CATEGORY </b> <br>
<b style="color:darkgreen">Old Category</b>
<br>
<select name="cat[]" multiple>
<?php
$query="select * from category ";
$res=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($res))
{
echo "<option>".$row['cat_nm'];
echo "<option>".$row['cat_id'];
}
?>
</select>
<br>
<b style="color:darkgreen">New Category</b><br>
<input type='text' name='cat[0]'></input><br>
<input type='text' name='cat[1]'></input>
<input type='submit' value=' UPDATE '>
</form>
I want to update the category name with another category name by its category id or by its category name. I get undefined index cat[0] and cat[1]
When you end an input name with [] it wil be converted to an array by php. The correct way to get the values in this case would be something like this:
$cat=$_POST['cat'];
$cat_nm=$cat[0];
$cat_id=$cat[1];
I combined the two routines into one script.
I added 'sub' to the form to distinguish from when the form was submitted or not.
I used list() in the query result loop.Used mysqli_fetch_array($result, MYSQLI_NUM) rather than mysqli_fetch_assoc($res)
used foreach() to loop through the $_POST['cat']
Added 'value' to the <option value=""> to hold the id
Eliminated the switching from HTML mode to PHP mode by using HEREDOC.
<?php
if (intval($_POST['sub']) == 1){
$newcat = $_POST['new'];
foreach($_POST['cat'] as $key=>$value){
if(strlen($newcat[$key]) > 0){
mysqli_query($conn,"UPDATE `category` SET `cat_nm`='$newcat[$key]' WHERE `cat_id`='$value'");
}
}
}
echo <<<EOT
<html><head><style>h4,h3{color:darkgreen;margin:.2em;}</style></head><body>
<form action="#" method='POST'>
<h3>UPDATE CATEGORY</h3>
<h4>Old Category</h3>
<select name="cat[]" multiple>
EOT;
$sql="SELECT `cat_nm`, `cat_id` FROM `category` ";
$result=mysqli_query($conn,$sql);
while(list($cat_nm,$cat_id) = mysqli_fetch_array($result, MYSQLI_NUM)){
echo " <option value=\"$cat_id\">$cat_nm</option>\n";
}
echo <<<EOT
</select>
<h3>New Category</h3>
<input type="text" name="new[0]" /><br/>
<input type="text" name="new[1]" /><br/>
<input type="hidden" name="sub" value="1" /><br/>
<input type="submit" value=" UPDATE />
</form>
</body></html>
EOT;
?>

Increment id on click in MySQL

I have a problem to visualize the solution for the problem that I have now.
The user is allowed to insert a row in a table.
And I try to display a button (input) +1 who allow the user to increment a column (vote) in a selected row among all created.
The problem is that I don't get the thing for rely incrementation to the desired id.
Here my code :
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['mainsubmit']))
{
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease(name) VALUES(:name)');
$req->execute(array('name' => $nameDisease));
}
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch())
{
$id = $result['id'];
echo $id ?>
<form action="" method="post"> <input name="secondsubmit" type="submit" value="+1"> </form><?php
if(isset($_POST['secondsubmit']))
{
$db->exec("UPDATE disease SET vote = vote + 1 WHERE id = " .$id);
}
}
Logically, the code above doesn't work but I don't understand how find the solution.
In brief, i want to allow the user to increment a column in a selected row.
Thanks
Edit: Shadow, it's not my problem because your solution is used for automatically chose between INSERT or UPDATE if the line doesn't exist or exist. Me, I want allow the user to create rows and allow he to vote +1 on each of one that exist, and it will not be possible for he to insert a row from the input +1.
I created code snippet similar to your code style.
You have two submit buttons so you need to separate handling of those two requests.
The $id of the item you want to update in the second submit need's to come from hidden value in form.
In order for this to work you need to create table in mysql:
create table disease (id MEDIUMINT NOT NULL AUTO_INCREMENT, name VARCHAR(20), vote INTEGER, PRIMARY KEY (id)); - for example like this
<html>
<body>
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
$db = new PDO('mysql:dbname=phpapp;host=db', 'root', 'phpapptest');
if (isset($_POST['mainsubmit'])) {
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease (name, vote) VALUES(:name, 0)');
$req->bindParam(':name', $nameDisease);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) { ?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
if (isset($_POST['secondsubmit'])) {
$req = $db->prepare("UPDATE disease SET vote = vote + 1 WHERE id = " . $_POST['id']);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) {?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
?>

PHP - post foreach content

So, i have this foreach loop that runs a sql query. Every row is printed in a option. I have two forum posts. One that 'deletes' the row and one that 'uses' the row. But when I post the form the content inside the option remains empty. Here is my code:
Post file
<?php
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$sql = "SELECT * FROM message";
?>
<html>
<form action="action.php" method="post">
<select><?php
foreach ($DB->query($sql) as $row)
{
?>
<option name="content" value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br /><input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Action.php
<?php
require_once 'hidden/session.php';
$delete = $_POST['delete'];
$use = $_POST['use'];
$content = $_POST['content'];
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$delete = "DELETE FROM message WHERE title='$content'";
if (isset($delete)){
$DB->exec($delete);
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
In order to post content you have to give name to your control. You have specified name property for option but there is no name property specified in select. Which is why the value of that control is not getting posted. Hence, when you try to access it as $content = $_POST['content']; , it gives you empty value.
Try this:
<html>
<form action="action.php" method="post">
<select id="content" name="content">
<?php foreach ($DB->query($sql) as $row) { ?>
<option value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br />
<input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Hope it helps!!

PHP - Can't update information from table

I can't change the information from my database.
I'm trying to update information from my database. But it's not working.
The user just need to put one existing ID from the table, and it will update the information from the specific column, in this case, it's a column called "titulo".
Here my page code:
<?php
if(isset ($_POST['submit']))
{
include("../classes/administrador/Administrador.class.php");
$Administrador = new Administrador();
if ($Administrador->atualizarCD($_REQUEST['id'],$_REQUEST['titulo']))
{
echo "It works !!!<br>";
} else {
echo "its not working<br>";
}
$Administrador->endAdministrador();
}
else{
?>
<b>Alterar CD</b><br><br>
<form name="form3" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<b>ID</b>:
<input type="text" name="id" size="3">
<br>
<br>
<b>Titulo</b>:
<input type="text" name="titulo">
<br>
<input type="submit" name="submit" value="submit">
</form>
<p> </p>
<?php
}
?>
And my function code:
function atualizarCD($id, $titulo) {
$sql= "UPDATE `cds` SET `titulo` = '$titulo' WHERE `ID` = '$id";
if($this->bd->executarSQL($sql)) return true;
else return false;
}
You have missed to add ' end of the query
$sql= "UPDATE `cds` SET `titulo` = '$titulo' WHERE `ID` = '$id' ";

php form arrays and sqlite - updated

I need some help I am trying to create a PHP form using sqlite3 database. I am looking up values from from an existing sqlite3 database in the "lookuptable" where the column "id = 340" and display those values as a dropdown selection. Then once the value is selected by the user then the form is submitted by the user which updates the new value in the "roster" table with the values from the php form. I get it to display the names in the dropdown but when I click on the update button to submit the data it updates what the value is in the array.
How do I post "firstname" and "lastname" from the user to the roster table instead of of the number on the array table?
PHP entry page Code:
<html>
<head>
<title></title>
</head>
<div class = "controlbox">
<body style="font-size:12;font-family:verdana">
<form action="post.php" method="post">
<p>
<h1> </h1>
<br>
<br>
Person : <select name="name">
<option>--Available Options--</option>
<?php
try
{
$db = new PDO("sqlite:DefaultLibrary.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
echo $e->getMessage();
}
$stmt2 = $db->query ("SELECT * FROM lookuptable where ID = '340' ");
$rowarray = $stmt2->fetchall(PDO::FETCH_ASSOC);
$cntr = 0;
foreach($rowarray as $row)
{
echo "<option value = $cntr >$row[FirstName] $row[LastName]</option>";
$cntr++;
}
?>
</select><br>
<p>
<input type="submit" name="update" value="update">
</p>
</form>
</body>
</html>
PHP Code: Post.php
<?php
$name = sqlite_escape_string($_POST['name']);
try
{
$db = new PDO("sqlite:DefaultLibrary.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
echo $e->getMessage();
}
if (!empty($person)) {
try
{
$stmt = $db->prepare("UPDATE roster SET rotationplace = :name WHERE ID = '340'");
$stmt->bindParam(':name', $name,PDO::PARAM_STR);
$stmt->execute();
}
catch(Exception $e)
{
echo $e->getMessage();
}
echo "submitted successfully";
}
?>
Try:
echo "<option value = $INSERT_NAME_HERE_NOT_COUNTER >$row[FirstName] $row[LastName]</option>";

Categories