I have two tables:
contacts
phonetypes
In my add action, i populate a dropdown box from the phonetypes table (which has only 1 column, namely phone_type).
My contacts table has the fields: l_name, f_name, phone_type and number.
I have the dropdown's displayField as phone_type. Based on the selection in the dropdown I try to insert the phone_types field in the contacts table, but it inserts the id from the phonetypes table. But I want to insert the values and not the id...
Any help is appreciated.
My view code:
echo $this->Form->create('Contact');
echo $this->Form->input('last_name', array('type'=>'text', 'size'=>10));
echo $this->Form->input('first_name');
echo $this->Form->select('phonetypes.phone_type',array('phone_type'=>'phone_type','options'=>$phone_type,'default'=>'phone_type'));
echo $this->Form->input('phone_number');
echo $this->Form->end('Save Entry');
The better question is, if you are not using an ID for the primary key in the phone types table, why even use the database, why not just use a static array? If you want to use the database, you should ad the ID column and store the ID in the contacts table that references the phone type. Then to get the drop data you would do the following:
$this->set('phone_types', $this->PhoneType->find('list));
Then in the view, the form field will look like:
echo $this->Form->input('phone_type', array('options'=> $phone_types));
If you do NOT want to use the ID in the table, then just eliminate the table and set up a static array:
$this->set('phone_types', array('Home' => 'Home', 'Cell' => 'Cell', 'Work' => 'Work'));
Related
I am trying to make a simple CRM in php and mysql. But stuck at a portion.
I have a table of users which can log-in at same time and access the contacts table all at a time. But I want to assign different range of data to them. For Example: if 'A', 'B' & 'C' all logs-in at the same time and access the table 'contacts' in front end, I want
'A' to show data from 1-500.
'B' - 501-1000. &
'C' - 1001-1500 etc.
If someone can help me in the right direction to how to achieve this, I'll be very thankful.
Finally Figured it out.
I created three input fields and one more column in contacts table named 'assigned_to' with default value of '0'.
The field are -
Select field for employee ID
Min(input[type=text]) range field
Max(input[type=text]) range field
I populated the MIN range and MAX range field using query
select MIN(id) as minid from contacts_table WHERE assigned_to=0
select MAX(id) as maxid from contacts_table WHERE assigned_to=0
Then upon post of the form I inserted the employee_id in the assigned_to column of contact table.
<?php if (isset($_POST['assign'])) {
$sel_emp = $_POST['sel_emp'];
$min_range = $_POST['min_range'];
$max_range = $_POST['max_range'];
$sqli = "UPDATE contacts_table SET `assigned_to`='$sel_emp' WHERE `assigned_to`='0' AND id BETWEEN '$min_range' AND '$max_range'";
if (mysqli_query($conn, $sqli)) {
?>
<script>
$("#msg").html("Assigned");
$("#msg").css('color', 'green');
</script>
<?php
} else {
echo mysqli_error($conn);
}
} ?>
So basically, the main query to perform the above said operation was
UPDATE leads SET `assigned_to`='251' WHERE `assigned_to`='0' AND id BETWEEN '1' AND '500'
This updated the assigned_to value with employee_id which is 251.
If anyone can provide a better solution. I'll be very thankful.
I'm using Laravel 5.1 , I've a model Customer which has many Vehicles.
I set validations for Vehicle model like this :
public static $Rules = array(
'code' => 'required|unique:vehicles',
'registernumber' => 'required|unique:vehicles'
);
Till now, all is fine : I can't insert two vehicles with the same code or registernumber.
What I want to do is :
Can I set a custom validation which allows me to insert unique code or registernumber value just for a given CustumerID ?
Example :
Customer1 :
Vehicle1: code1, registernumber1
Vehicle2: code2, registernumber2
(Here I can't insert for example two codes having 'code1' value with Customer1)
Customer2 :
Vehicle1: code1, registernumber1
Vehicle2: code5, registernumber5
(Here I can't insert for example two registernumbers having 'registernumber5' value with Customer2)
Any idea please ?
As long as the customer id is in that table. The way the unique validation rule works is as follows:
unique:
[table name, optionally with a connection name],
[the column you are checking for uniqueness, optional],
[the id of the row that will be ignored in the check (for example if you are updating a row, you want the row you are updating to not be included), optional],
[the column name that contains the id you are running the check against, optional],
[additional where clauses (where_column, where_value)]
So, if your table schema looks like this:
vehicle_id, code_number, registration_number, customer_id
And you only want to run the unique check for rows of the same customer, you can do this:
$customer_id = 'whatever';
$Rules = array(
'code' => 'required|unique:vehicles,code_number,NULL,vehicle_id,customer_id,'.$customer_id
);
That will run the check only on rows where('customer_id', $customer_id)
Syntax
ALTER TABLE `tablename`
ADD UNIQUE KEY `my_unique_key` (`colname1`, `colname2`);
In your example
ALTER TABLE `yourtable`
ADD UNIQUE KEY `unique_reg` (`customername`, `vehicle`, `code`, `regno`);
Try this but you should handle the db_error otherwise it affects the user experience
I am using this auto complete form, that gets the data from 1 table,
now i am using that form to insert data from its table to another table.
here is my SQL for the inserting into the table "products"
$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','".mysql_real_escape_string($_POST['prod_name'])."','$_POST[prod_category]' ,'$_POST[prod_price]',
'".mysql_real_escape_string($_POST['prod_desc'])."','$_POST[prod_quantity]','{$image}')";
the prod_category is the column i need to fill. I have data from the table named "categories" with column name "categories"
so how do i input the data from categories to the column = prod_category in the products table?
Check example and try this way...may it's help you.
INSERT INTO student (s_id, s_name, s_email)
SELECT t_id, t_name, t_email FROM teacher
WHERE teaher.tid='25';
As i understand in POST['prod_category '] contain string, like you have text input. The best way to do what you need - is change category for select in html, like this
<select>
foreach(categories as $category){
<option value='category->id' >category1->name </option>
}
</select>
Then you will get in post category id from the table named "categories"
If you dont like it your should replace '$_POST[prod_category]' from youre query to subquery
select id from categories where categories = '$_POST[prod_category]'
I need to insert data into one table and references data into another table in ZF2 using tableGateway.
For example, when I am registering a user, I have to insert user data into one table and this user hobbies data(Multiple rows) into another table with the references of the inserted user id and Update data also should work.
I have referred this url:
Want to insert into two tables using one form in ZF2
But this won't help me.
Suppose we are in 'user' model. So by default tableGateway will insert data in user table and for hobbies table, I have instansiated new tableGateway as '$userTable'.
$data = array(
'id' => $user->id,
'name' => $user->name,
);
$this->tableGateway->insert($data); //this will insert data in user table
$last_id=$this->tableGateway->lastInsertValue; //getting last inserted id
$adapter=$this->tableGateway->getAdapter();
$userTable = new TableGateway('hobbies', $adapter); //this will insert in hobbies table.
$data_arr = array(
'link_id' => $last_id,
'music_info' =>'test',
);
$artistTable->insert($data_arr);
What is the proper way to automatically create new tables based on values in another table? For instance, If table A has a column named city that contains different city values then I would need to create a new table based on each different city. Then all records with the respective city needs to be inserted into it's respective table. Also, if the city contains a space in the name it needs to be replaced with a an underscore. How could the same be done in MySQL?
In MS ACCESS I could accomplish this by:
Using A Select And Replace Query Named SELREP
SELECT table_A.column1, table_A.column2, table_A.city, Replace([city]," ","_") AS table_name_column FROM table_A;
Create a Public Function MakeTableCity
Public Function MakeTableCity()
DoCmd.SetWarnings False
Dim db As Database
Set db = Application.CurrentDb
Dim distinctValues As DAO.Recordset
Set distinctValues = db.OpenRecordset("SELECT table_name_column FROM SELREP GROUP BY table_name_column", dbOpenSnapshot)
Do Until distinctValues.EOF
DoCmd.RunSQL "SELECT * INTO " & distinctValues("table_name_column") & " FROM SELREP WHERE table_name_column ='" & distinctValues("table_name_column") & "'"
distinctValues.MoveNext
Loop
DoCmd.SetWarnings True
Set distinctValues = Nothing
Set db = Nothing
End Function
If you are planning to create a new
table city with data about cities,
meaning one row for every city, then
go ahead and read the answer.
If , on the other hand, you are
planning to make a new table for
every city, with identical columns,
then your plan is very bad design.
Read about normalization first.
First alternative is to create a table named city with fields that you want. Example:
CREATE TABLE city
( id INT auto_increment PRIMARY KEY
, name VARCHAR(50) NOT NULL
, population INT
, state CHAR(2)
) ;
Then copy the different city names into it with:
INSERT INTO city (name)
( SELECT DINSTINCT city --- change "city" into REPLACE(city, ' ', '_')
FROM table_A --- for the small changes you want
) ;
Then, update the other fields (population, state, etc).
If no two cities have same name, the JOINs between the two tables can then be done using ON table_A.city = city.name
If not, (and better anyway as the Primary Key of city will be smaller), you may ALTER the structure of table table_A by adding a field cityid and dropping the city field. Then the JOINs between the two tables will be done using ON table_A.cityid = city.id
Second option is to directly create table city with:
CREATE TABLE city AS
( SELECT DINSTINCT city AS name --- change "city" into REPLACE(city, ' ', '_')
FROM table_A --- for the small changes you want
) ;
and then alter the table defining Primary Key, adding (population, state, etc).