How to add comments to post with Mysql and PHP - php

I managed to add comments to the table of Comments but it doesn't appear to the related post. I understand because the foreign key (artID) in the articles table is empty. How do I fetch the primary key from the articles table? Here is the structure of my database.
SQL injections issues will be dealt later. Prepared statements will be done. I just would like to get some help on the query and php function. Thank you.
articles
artID
artTitre
artAuteur
artContenu
artDate
commentaires
commentID
commentPseudo
commentText
artID
commentaires.sql.php
<?php
// INSERT
function insertCommentaire($c){
$PseudoCommentaire = $TexteCommentaire ='';
$PseudoCommentaire = $_POST['PseudoCommentaire'];
$TexteCommentaire = $_POST['TexteCommentaire'];
$IdArticle = $_POST['IdArticle'];
$qryInsertComm = 'INSERT INTO commentaires (commentPseudo,commentText, artID)
VALUES ( \''.$PseudoCommentaire.'\',
\''.$TexteCommentaire.'\',
\''.$IdArticle.'\')
';
if (!mysqli_query($c,$qryInsertComm))
{
die('Error: ' . mysqli_error($c));
}
echo "1 record added";
}
// UPDATE
function updateCommentaire( $IdCommentaire ){
}
// DELETE
function deleteCommentaire( $IdCommentaire ){
}
// CONTROLER //
switch( $action ){
case 'insert' :
$process = insertCommentaire($conn);
if( $process == 'ok' )
header( 'location:index.php?page=home' );
else
$page = 'home';
break;
case 'update' :
$process = updateCommentaire( $_GET[ 'item' ] );
if( $process == 'ok' )
header( 'location:index.php?page=home' );
else
$page = 'home';
break;
case 'delete' :
$process = deleteCommentaire( $_GET[ 'item' ] );
if( $process == 'ok' )
header( 'location:index.php?page=home' );
break;
}
?>

You are using a hidden input val called IdArticle in
<input type="hidden" name="IdArticle" value="" />
To keep track of which article the comment should go against, as shown above in the php script:
$IdArticle = $_POST['IdArticle'];
However, looking at your HTML, you should see that you are passing an empty value to your php script.
To fix this, update your hidden input field like this:
<input type="hidden" name="IdArticle" value="<?php echo $rows['artID'] ?>" />
Now it should work.

How do I fetch the primary key from the articles table?
The same as selecting any other column
SELECT `artID` FROM `articles` WHERE `artID` = ?
That's the only question I can see in your post.

Related

problem with else if statement not working

I have created a custom WordPress filter. I am importing api data to a form. My first IF statement works (when district not empty). But I can't get the else if working to populate the 2nd field. The 2nd result in the JSON has no value for district. What I am trying to do is separate the results to display House rep in one field and Senators in another. (There are actually two Senate results but I don't now how to separate the results). Each result needs to be in different form fields.
add_filter('frm_pre_create_entry', 'import_fields');
function import_fields($values){
if ( $values['form_id'] == 12 ) {
$zipcode = $values['item_meta'][71];
$api_request = 'http://whoismyrepresentative.com/getall_mems.php?zip='.$zipcode.'&output=json';
$api_response = wp_remote_get( $api_request );
$api_data = json_decode( wp_remote_retrieve_body( $api_response ), true );
foreach ( $api_data["results"] as $member ) {
if ( $member["district"] != '' ) {
$values['item_meta'][72] = $member['name'];
$values['item_meta'][73] = $member['phone'];
$values['item_meta'][74] = $member['office'];
} else if ( $member["district"] == '' ) {
$values['item_meta'][75] = $member['name'];
$values['item_meta'][76] = $member['phone'];
$values['item_meta'][77] = $member['office'];
}
break;
}
}
return $values;
}
I am new here, please go easy on me.
just like #billyonecan said, that break looks weird.
PS: You don't even need to specify the 'else if' condition... simply replace the 'else if' by 'else'
...
} else if ( $member["district"] == '' ) {
...
becomes
...
} else {
...
In your case it won't make any difference, it's just good practices

How to combine two if statements in PHP for WP

I need to run a function (show a modal) if a page DOES have a particular field value AND is not a certain page. It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it is not the certain page. They work individually like this:
//if page has field value:
<?php
$values = get_field( 'recast_video' );
if ( ($values) ) {
get_template_part('template-parts/popIn', 'none');
}
?>
//if it is this page, do not show:
<?php
if ( (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
How do I combine the two so that it shows (get_template_part) if the field has the value and is not the specified page
//this fails
<?php
$values = get_field( 'recast_video' );
if ( ($values) || (!is_page('trading-education-webinars') ) ){
get_template_part('template-parts/popIn', 'none');
}
?>
First thing to do is look for the common condition. No matter what, you don't want it to be a certain page, right? So first check that it's not that page:
$result = (!is_page('trading-education-webinars')) ? : ;
It also needs to work if the page DOES NOT HAVE THE FIELD VALUE and it
is not the certain page.
Then why check for the field value at all? No need, as long as it's not the page we checked for.
$result = (!is_page('trading-education-webinars')) ? get_template_part('template-parts/popIn', 'none') : return false;
You can change return false; to whatever you want to happen if the page IS 'trading-education-webinars'
EDIT: Clarifying the conditional:
Page X never gets served "content" (modal)
If NOT page X and has $values, show content
If NOT page X and NOT has $values, show some other content
$values = get_field( 'recast_video' );
$x = get_template_part('template-parts/popIn', 'none');
$y = 'some other content';
$return = (!is_page('trading-education-webinars')) ? (($values) ? $x; : $y ) : return false );
Use the && (AND) operator:
<?php
$values = get_field( 'recast_video' );
if ( ($values && !is_page('trading-education-webinars')) || (!$values && is_page('trading-education-webinars')) ){
get_template_part('template-parts/popIn', 'none');
}
?>
|| operator is true when at least one or both statements are true

PHP write a conditional statement based on whether an argument is in a database or not

I am developing a wordpress plugin. There is a variable that I send to the database once a payment has been made, that is ispayment and it is set to 1. If the payment has not been made, then the variable is not sent to the database...so there is an instance where, for a particular post ID, the variable "ispayment" does not exist.
I'm now trying to write a conditional statement based on whether this variable is present or not.
What I'm trying to say is, if wedding_form_final_submit = 1 AND there is no ispayment variable - $status = foo. Else, if wedding_form_final_submit = 1 AND ispayment = 1, $status = bar.
Here is what I have so far:
if(get_post_meta($post_id,'wedding_form_final_submit', 1) && get_post_meta($post_id, 'ispayment',false)) {
$status = 'Form Complete';
} elseif(get_post_meta($post_id,'wedding_form_final_submit', 1) && get_post_meta($post_id, 'ispayment',true)) {
$status = 'Deposit Paid';
} else {
$status = '';
}
Currently, this just returns "Form Complete" but it's for an entry that should return Deposit Paid, the entry that should say Form Complete is just blank.
I believe my issue is that for my first statement, the variable ispayment doesn't exist. How do I ammend this though?
This code below will probably do, if the post meta you're retrieving doesn't exist, your variable just return empty
// get post meta value 'ispayment'
$paid = get_post_meta( $post_id, 'ispayment', true );
//Check where post meta exists and value is 1
if ( isset($paid) && $paid === '1' ) {
$status = 'Form Complete';
//If post meta doesn't exist or value is not 1.
} else {
$status = 'Deposit Paid';
}
//or shorter
$paid = get_post_meta( $post_id, 'ispayment', true );
$status = ( isset($paid) && $paid === '1' ) ? 'Form Complete' : 'Deposit Paid';
just add your other code in there

PHP/SQL syntax: passing variable to SQL query

I am getting tired trying to see what is wrong. I have two php. From the first I am sending a variable 'select1' (basically the id) to the second and than I want to update that record uploading a pdf file.
$id = "-1";
if (isset($_GET['select1'])) {
$id = mysql_real_escape_string($_GET['select1']);
}
if(isset($_POST['Submit'])) {
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
if ($my_upload->upload()) { // new name is an additional filename information, use this to rename the uploaded file
mysql_query(sprintf("UPDATE sarcini1 SET file_name = '%s' WHERE id_sarcina = '%s'", $my_upload->file_copy, $id));
}
}
If I put a line with a valid id, like:
$id = 14;
it is working. What I am doing wrong? Thank you!
If you need to accept both post & get, then you should try something like the code below to retrieve the variable.
$var = 'select1';
if( isset( $_POST[$var] ) ) {
$id = $_POST[$var];
} else if( isset( $_GET[$var] ) ) {
$id = $_GET[$var];
} else {
$id = -1;
}
You are using both GET and POST at the same time. As far as I can see, this condition is not returning True
if (isset($_GET['select1']))
Edit: If you don't find any answer in above; maybe some more information/code can help getting to a solution.

Php checkbox retrieve data from database

I have:
$array_worker['$worker_id']=$worker_name;
$array_job['$job_id']=$job_name;
I have no problem with dynamic create table with checkbox and store data in database:
The data are stored in table as worker_id,job_id!
Normally, worker may work more than one job, so I create multidimensional array from table in which the stored data!
$array_worded['$worker_id'][]=$job_id;
My question is:
How create dynamic table with checked checkboxes based on array_worked array?
$table='';
foreach($array_worker as $key=>$value){
$table.=''.$value.''; // worker name
$worker_id = // get worker id from $array_worker
foreach($array_job as $key_job=>$val_job)
{
$job_id = // get job id from $array_job
$checked = false;
foreach( $array_worked[$worker_id] as $key_worked => $val_worked )
{
if( $job_id == $val_worked ) // $val_worked contains $job_id
{
$checked = true;
break;
}
}
$table.='<input type="checkbox"' . ( $checked ? ' checked="checked"' : '') . '/>'.$val_job.''; // all jobs from database
}
$table.='';
}
$table.='';
I may make some mistakes in syntax, but code demonstrates basic principle.
It is so simple :
<input type="checkbox" name="formWheelchair"
<?php
$DATABASE-VALUE = $array_worded['$worker_id'][] = $job_id; // OR WHAT EVER
switch ($DATABASE-VALUE) {
case 0:
echo checked />"
break;
........
}
?>

Categories