Insert data into Wordpress database table from a custom form
I have worked with your example but I have a problem. I have error on the start. I get this message:
WordPress database error: [Cannot add or update a child row: a foreign
key constraint fails (prowebex_barbara.barbara_schedule, CONSTRAINT
barbara_schedule_ibfk_1 FOREIGN KEY (day_id) REFERENCES barbara_days
(id_day))]
INSERT INTO barbara_schedule (id, challenger, challenged,
date_match, day_id, term_start, term_end) VALUES ('', 'gfdgdfgdfg', 'gdfgfdgdf', '2015-11-30', '', '23:00', '00:59');
Data Submitted
Can you tell me how can I change this. I have already, truncate tables,delete both tables. I don't know what to do.
<?php
if(isset($_POST['submit'])) {
global $wpdb;
$id = sanitize_text_field($_POST['id']);
$challenger = sanitize_text_field($_POST['challenger']);
$challenged = sanitize_text_field($_POST['challenged']);
$date_match = sanitize_text_field($_POST['date_match']);
$day = sanitize_text_field($_POST['day_id']);
$term_start = sanitize_text_field($_POST['term_start']);
$term_end = sanitize_text_field($_POST['term_end']);
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
$wpdb->insert($table_name, array(
'id' => $id,
'challenger' => $challenger,
'challenged' => $challenged,
'date_match' => $date_match,
'day_id' => $day,
'term_start' => $term_start,
'term_end' => $term_end,
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
$msg = "Data Submited";
echo $msg;
}?>
<form action="" method="post" id="subForm">
<div>
<input type="hidden" name="id" id="iiihuu-iiihuu" placeholder="" />
<br>
<input type="text" name="challenger" id="iiihuu-iiihuu" placeholder="Izazivač" />
<br>
<input type="text" name="challenged" id="iiihuu-iiihuu" placeholder="Izazvani" />
<br>
<input type="date" name="date_match" id="iiihuu-iiihuu" placeholder="Datum" />
<br>
<select>
<option>I don't know how to do this</option>
</select>
<br>
<input type="time" name="term_start" id="iiihuu-iiihuu" placeholder="Termin od" />
<br>
<input type="time" name="term_end" id="iiihuu-iiihuu" placeholder="Termin do" />
<br>
<input type="submit" name="submit" value="Spasi" class ="submit_button" id="formSubmit" />
</div>
</form>
</div>
The errors look like that the custom tables actually have relationship with each other, not sure how the database structure is, but it looks like in order to create a record in barbara_schedule you will have to also create a record in barbara_days.
So in order to just check if this is the case you should pass a fake day_id. So replace $_POST['day_id'] with an existing day_id that already exists in the barbara_days table.
But if thats not the case, then here is a quick thought, do you have a require statement on the top of this file?
Usually this is needed in order to make an insert on custom tables besides the meta table.
require_once('LINK-TO/wp-load.php');
Secondly i am not sure, what this outputs
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
but just in case, try to hardcode the table name like this
$table_name = $wpdb->prefix."table-name"
Related
I want to insert data selected from check boxes into a database as well as text which the user types into a text box. I want both the check box values and the text to go into the same row on my database.
My code for abc.php:
<div class="container">
<h2>Please select the answer below:</h2>
<form>
<div class="checkbox">
<label><input type="checkbox" name="response" value="A">A</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="response" value="B">B</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="response" value="C">C</label>
</div>
<form method="post" name="input" action="insert.php" >
Question ID: <input name="questionid" type="text"/><br/>
<input type="submit" name="Submit" value="insert" />
</form>
my code for insert.php
if(isset($_POST["response"]))
{
$query = "INSERT INTO response (student_id, response, question_id) VALUES (:studentID, :response, :questionid)";
$statement = $conn->prepare($query);
$statement->execute(
array(
':response' => $_POST["response"],
':studentID' => $_SESSION['studentid'],
':questionid' => $_POST["questionid"]
)
);
}
When i press the insert button, nothing happens..... can anyone help?
<form action="uploads.php" method="post" enctype="multipart/form-data" id="upload" class="upload">
<fieldset>
<legend>Upload</legend><br/>
Title: <input type="text" name="name" id="name" class="name" required> <br/><br/>
<textarea name="description" rows="6" cols="35" maxlength="120"></textarea><br/>
<input type="file" id="file" name="file[]" required multiple> <br/>
<input type="submit" id="submit" name="submit" value="Upload">
</fieldset>
<div class="bar">
<span class="bar-fill" id="pb"><span class="bar-fill-text" id="pt"></span></span>
</div>
<div id="uploads" class="uploads">
Uploaded file links will appear here.
</div>
<?php
// configuration
$dbhost = "localhost";
$dbname = "blog";
$dbuser = "root";
$dbpass = "pass";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$name = 'name';
$mime = 'mime';
$data = 'data';
$size = 'size';
$description = 'description';
$created = 'created';
$url = 'url';
// query
$sql = "INSERT INTO videos (name,mime,data,size,description,created,url) VALUES (:name,:mime,:data,:size,:description,:created,:url)";
$q = $conn->prepare($sql);
$q->execute(array(':name'=>$name,
':mime'=>$mime,
':data'=>$data,
':size'=>$size,
':description'=>$description,
':created'=>$created,
':url'=>$url));
?>
I'm not so good with PDO, I can get videos to upload to my db, but I can't take in a name or anything. It just shows: name, description, size is 0 and etc. I've watched a few tutorials, but none of them show how to add it by what the user names it or describes it as, only what they put into the values goes to the database. I've also searched around on here and many other websites, but no luck.
I have managed to save a custom meta field with multiple inputs to a post. The inputs are going like this:
<input type="text" name="menuitem[1][title]" />
<input type="text" name="menuitem[1][section]" />
<input type="text" name="menuitem[1][price]" />
<input type="text" name="menuitem[2][title]" />
<input type="text" name="menuitem[2][section]" />
<input type="text" name="menuitem[2][price]" /> etc.
And the data are stored in serialized arrays.
The reason I do it this way, is because I want to query the values of this field, related to each other. (e.g. to query posts sorted by menuitem[price] of menuitem[title]).
The problem is that WP-query can only read a single key value and cannot read serialized data.
Is there any better way to store meta keys so they are related to each other?
Any suggestions would be appriciated.
Thank you
You have to use foreach
Example:
<?php
$entries = get_post_meta($post->ID, '_your_metabox_group_id', true );
foreach ( (array) $entries as $key => $entry ) {
$title = $section = $price = '';
if ( isset( $entry['_your_title_meta_id'] ) )
$title = get_post_meta($post->ID, '_your_title_meta_id', true);
if ( isset( $entry['_your_section_meta_id'] ) )
$section = get_post_meta($post->ID, '_your_section_meta_id', true);
if ( isset( $entry['_your_price_meta_id'] ) )
$price = get_post_meta($post->ID, '_your_price_meta_id', true);
// your output e.g
// echo '<span class="some-class">'.$title.'</span>';
}
?>
I have the following code for my login view:
<?php
echo $session->flash('auth');
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
This generates the following HTML:
<div id="authMessage" class="message">Login failed. Invalid username or password.</div>
<form id="UserLoginForm" method="post" action="/control/users/login" accept-charset="utf-8">
<div style="display: none;"><input name="_method" value="POST" type="hidden"></div>
<div class="input text required">
<label for="UserEmail">Email</label>
<input name="data[User][email]" maxlength="255" value="" id="UserEmail" type="text">
</div>
<div class="input password">
<label for="UserPassword">Password</label>
<input name="data[User][password]" id="UserPassword" type="password">
</div>
<div class="submit"><input value="Login" type="submit"></div>
</form>
App Controller:
function beforeFilter()
{
$this->Auth->fields = array (
'username' => 'email',
'password' => 'password'
);
}
When I attempt to log in I get an error that my password is incorrect. Here's a dump of $this->data:
Array
(
[User] => Array
(
[email] => myemail#gmail.com
[password] =>
)
)
I found a few other similar questions on stackoverflow but none of them seem to have an answer. Does anyone know why this is happening?
The cakePHP Auth component will hash your password and try to match with the password in DB.
They are hashing the password with the salt.
If you are setHash to MD5 then
MD5($password.$salt) will be stored matched with the password in DB.
So plz make sure that you are storing the password is correct.
To enter some dummy data. i usually try to debug $this->Auth->password($password) Then i will copy the result and insert in DB.
If the password is wrong the Auth component will reset the password field internally.ie., why u get this result.
Array
(
[User] => Array
(
[email] => myemail#gmail.com
[password] =>
)
)
This doesn't means your password is not available for checking with DB.
Happy Baking
I'want to render:
<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />
but Zend_Form_Element require a (string) name, so I need to do:
$this->addElement('text', '1', array(
'belongsTo' => 'foo'
));
$this->addElement('text', '2', array(
'belongsTo' => 'bar'
));
but the output is:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2" type="text" value="" name="bar[2]" />
I can also accept an output like:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1" type="text" value="" name="bar[1]" />
but Zend_Form_Element rewrite elements with the same name
is there a way to do what I need?
For multiple values:
$foo = new Zend_Form_Element_Text('foo');
// Other parameters
$foo->setIsArray(TRUE);
$this->addElement($foo);
Generates: name="foo[]"
--
If you're looking for given keys such as name="foo[bar]", use:
$bar= new Zend_Form_Element_Text('bar');
// Other parameters
$bar->setBelongsTo('foo');
$this->addElement($bar);
--
Tested on ZF 1.11.5
class MyFooForm extends Zend_Form {
public function init() {
$fullNameOpts = array(
'required'=>false,
'label'=>'fullName',
'isArray'=>true,
'validators' => array( array('stringLength', false, array(1, 250) ) )
);
$this->addElement('text' ,'fullName',$fullNameOpts);
// rest of the elements , forms and stuff goes here
}
}
And that does creates
<dd id="fullName-element"><input type="text" class="inputAccesible" value="" id="fullName"name="fullName[]"></dd>
It's on Element.php , in Form , line 512 "isArray" check.
I'm using a regular zend_form, crossValidation with custom validators and i'm pushing subforms to replicate the main form, 'cause the user can add multiple times the same form.
Additionally , I'm too lazy to research custom decorators, i have created one, but it kills subForms and array notation, so i just stick with the regular ones, and that solves it.
I'm at Zf 1.10.