last day (14 days later) » 

2:30 PM
0
A: Post category insert checkbox values into database

Gotrekkyou don't get to the second insert query because of // REDIRECT BACK TO DOCUMENT LIST echo("<script>location.href = 'list_doc.php';</script>"); try with: <?php $q = "SELECT * FROM cats"; $r = mysqli_query ($dbc, $q); // Run the query. echo '<div class="view_body">'; // FETCH AND P...

 
So now I have updated it tells me I have an undefined variable: Notice: Undefined index: cat_id in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 24 which refers to // POST CAT ID if ($_POST['cat_id']) { mysqli_real_escape_string($dbc, trim($_POST['cat_id'])); }
Sorry I didn't see your edit, I am still not getting it to post into the join table where on earth is this going on?
 
could you show me the whole content of $query please?
 
I have updated my question
 
ok, could you echo that $query variable before it's used?
 
Ok now I get Notice: Undefined index: cat_id in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 27 Notice: Undefined variable: query in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 45
Which is referring to the cat_id used in the $_POST query and also not being able to execute the $query as I killed the script
I hope that helped? Sorry I am really struggling with this
 
2:30 PM
check my edit and let me know what is in $cat_ids
 
2:40 PM
Hey Man
 
hi there
check my edit
and le me know if u get notice
 
Shall I take everything from where you say try this
 
there are both files
there was an error in the form aswell
 
ok ill try
 
lemme know
 
2:43 PM
So im getting the doc created still but nothing in the db for the doc_cat_join table
 
get me an echo of $cat_ids
and an echo of $query aswell
 
before its called?
 
after
    mysqli_query($dbc, $query);
    mysqli_close($dbc);
after this
 
just right at the end of the script?
ah ok
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28

Notice: Undefined variable: doc_id in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 50

Notice: Array to string conversion in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 61
ArrayINSERT INTO cat_doc_join (cat_id,doc_id) VALUES(0,)
 
you don't have a $doc_id declared anywhere
 
2:48 PM
i think this is the issue
there wont be one because it hasnt been created yet
 
what do you want to insert in cat_doc_join then ?
 
the new doc_id along with the cat_id selected
is there a way to do that?
 
can u dump the $cat_ids array
 
hm it wont let me get it
<?php
require ('../../db_con.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

// FORM VALIDATION & SUBMISSION
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array(); // BEGIN AN ERRORS ARRAY

// CHECK FIRST NAME
if (empty($_POST['doc_name'])) {
$errors[] = 'You forgot to enter a document name.';
} else {
$dn = mysqli_real_escape_string($dbc, trim($_POST['doc_name']));
}

// CHECK LAST NAME
if (empty($_POST['doc_content'])) {
$errors[] = 'You forgot to enter any document content.';
So imagine it being a new post like in wordpress
 
get my new edut
edit
 
2:53 PM
when did you update?
 
if (empty($errors)) {

// MAKING THE QUERY

$q = "INSERT INTO docs (doc_name, doc_content, created_date) VALUES ('$dn', '$dc', NOW() )";

$r = mysqli_query($dbc, $q);

if ($r) {
$doc_id = mysql_insert_id();
 
got it
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28

Deprecated: mysql_insert_id(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 41
array(1) { [0]=> int(0) }
 
are you checking any checkbox in the form ?
 
yeap there are two categories I can chose from
 
can you show me the $query again now?
 
2:57 PM
dump it out or echo it or?
Deprecated: mysql_insert_id(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 41
string(52) "INSERT INTO cat_doc_join (cat_id,doc_id) VALUES(0,0)"
 
damn i'm not on my computer
 
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28

Warning: mysqli_insert_id() expects exactly 1 parameter, 0 given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 41
string(51) "INSERT INTO cat_doc_join (cat_id,doc_id) VALUES(0,)"
 
i can't try it on myself
 
what argument is that expecting?
 
is it inserting anything in docs table?
$q = "INSERT INTO docs (doc_name, doc_content, created_date) VALUES ('$dn', '$dc', NOW() )";

$r = mysqli_query($dbc, $q);
 
2:59 PM
it is yep
 
echo $doc_id
pls
 
where abouts?
 
after
$r = mysqli_query($dbc, $q);

if ($r) {
$doc_id = mysql_insert_id();
 
nothing is eching out
think because its givine me those errors still
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28

Warning: mysqli_insert_id() expects exactly 1 parameter, 0 given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 42
heystring(51) "INSERT INTO cat_doc_join (cat_id,doc_id) VALUES(0,)"
 
does the table docs have an auto incremental id column ?
 
3:01 PM
yes
its called doc_id
 
change that $doc_id = mysql_insert_id();
to
$doc_id = mysqli_insert_id($dbc);
and check if u get anything in doc_id
 
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28
61string(53) "INSERT INTO cat_doc_join (cat_id,doc_id) VALUES(0,61)"
looks like im getting the value
but not getting the cat_id
 
ok we getting there :d
it was full of errors
 
for sure!
 
echo $cat_ids;
 
3:06 PM
sorry im new to this side of things
 
before
 
where do i echo that out?
 
 mysqli_query($dbc, $query);
    mysqli_close($dbc);
 
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28
62
Notice: Array to string conversion in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 62
Array
 
print_r($cat_ids);
sorry
 
3:09 PM
Warning: trim() expects parameter 1 to be string, array given in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 28
63Array ( [0] => 0 )
is it something to do with this..
// POST CAT ID
if ($_POST['cat_id']) {
    mysqli_real_escape_string($dbc, trim($_POST['cat_id']));
}
 
just comment that out for now
and put
print_r(array_keys($_POST['cat_id']));
always in the same place where u do all the echo
 
68Array ( [0] => 0 ) Array ( [0] => 0 )
 
show me the form again
 
<form action="new_doc.php" method="post">
<br>
<input style="width:50%;" placeholder="Document Name" type="text" name="doc_name" autocomplete="off" name="doc_name" value="<?php if(isset($_POST['doc_name'])) echo $_POST['doc_name']; ?>" />

</div>
</div>


<div class="row">
<div class="col-group-1">
<?php

$q = "SELECT * FROM cats";

$r = mysqli_query ($dbc, $q); // Run the query.

echo '<div class="view_body">';

// FETCH AND PRINT ALL THE RECORDS
while ($row = mysqli_fetch_array($r)) {
echo '<br><label><input type="checkbox" name="cat_id[]" value="' . $row['cat_id'] . '">' . $row["cat_name
 
if you inspect that checkbox element...
tell me what u see
 
3:17 PM
<input type="checkbox" name="cat_id[]" value="24">
<input type="checkbox" name="cat_id[]" value="25">
which reflect the correct ids from the category table i the db
 
then $_POST['cat_id'] should be an array with 24 and 25
 
it should be but it isnt getting those values is it not?
here is if i select both checkboxes
70Array ( [0] => 0 [1] => 1 ) Array ( [0] => 0 [1] => 1 )
 
try this
$checked = $_POST['cat_id'];
for($i=0; $i < count($cat_id); $i++){
echo "Selected " . $cat_id[$i] . "<br/>";
}
wait
$checked = $_POST['cat_id'];
for($i=0; $i < count($checked ); $i++){
echo "Selected " . $checked [$i] . "<br/>";
}
this one
sorry
 
where abouts?
 
always same place
 
3:22 PM
71Array ( [0] => 0 [1] => 1 ) Selected 24
Selected 25
Array ( [0] => 0 [1] => 1 )
 
give me the actual action php
i think we're done now
 
<?php
require ('../../db_con.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

// FORM VALIDATION & SUBMISSION
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array(); // BEGIN AN ERRORS ARRAY

// CHECK FIRST NAME
if (empty($_POST['doc_name'])) {
$errors[] = 'You forgot to enter a document name.';
} else {
$dn = mysqli_real_escape_string($dbc, trim($_POST['doc_name']));
}

// CHECK LAST NAME
if (empty($_POST['doc_content'])) {
$errors[] = 'You forgot to enter any document content.';
 
<?php
require ('../../db_con.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

// FORM VALIDATION & SUBMISSION
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array(); // BEGIN AN ERRORS ARRAY

// CHECK FIRST NAME
if (empty($_POST['doc_name'])) {
$errors[] = 'You forgot to enter a document name.';
} else {
$dn = mysqli_real_escape_string($dbc, trim($_POST['doc_name']));
}

// CHECK LAST NAME
if (empty($_POST['doc_content'])) {
$errors[] = 'You forgot to enter any document content.';
try now
 
oop its broke the script
hold up think theres a stray {
The Following error(s) have occured:

Notice: Undefined variable: errors in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 86

Warning: Invalid argument supplied for foreach() in C:\MAMP\htdocs\admin\actions\new_doc.inc.php on line 86
Please try again!.
 
aaargh i don't have any ide with me now :D
hold on
 
3:29 PM
its refering to this
foreach ($errors as $msg) {
 
<?php

require ('../../db_con.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

// FORM VALIDATION & SUBMISSION
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array(); // BEGIN AN ERRORS ARRAY
// CHECK FIRST NAME
if (empty($_POST['doc_name'])) {
$errors[] = 'You forgot to enter a document name.';
} else {
$dn = mysqli_real_escape_string($dbc, trim($_POST['doc_name']));
}

// CHECK LAST NAME
if (empty($_POST['doc_content'])) {
$errors[] = 'You forgot to enter any document content.';
 
ok so its showing no errors
BUT
nothing is hitting the join table
 
echo $query
always same place
 
INSERT INTO cat_doc_join (cat_id,doc_id) VALUES(24,75)
 
u checked 24 only as checkboxm right ?
 
3:33 PM
yup
 
        $query = "INSERT INTO cat_doc_join (cat_id,doc_id) VALUES ";
there is a missing space after VALUES
 
hmm
so this is selecting both...
INSERT INTO cat_doc_join (cat_id,doc_id) VALUES (24,79),(25,79)
i added a space in
 
and u get nothing into cat_doc_join ?
what columns does cat_doc_join have ?
 
iv done it
 
does it work ?
 
3:36 PM
it does man
thats genius
 
i'm sorry it took so long
but i have no ide or anything here with me now :D
could you paste the form and the action again
so i can post it back in stackoverflow as solution
 
man you have been the BIGGEST help ever
100% you can
ill big you up as much as I poss can
does it work too dissimilar for updating that table or is that a whole new ball game?
 
pretty much the same
i gotta go now
 
seriously thanks man
 
you welcome :)
just accept my solution and upvote please
 
3:39 PM
all done!
 
thanks
have fun :9
and good lucjk
 

last day (14 days later) »