Jump to content

SELECT and INSERT a String.


gacoksa

Recommended Posts

hi all, help me please.

CREATE TABLE `a_table` (
  `id` int(11) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `group` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
INSERT INTO `a_table` VALUES (1, '111', 'cd');
INSERT INTO `a_table` VALUES (2, '222', 'cd');
INSERT INTO `a_table` VALUES (3, '333', 'cd');
INSERT INTO `a_table` VALUES (4, '444', 'xy');
INSERT INTO `a_table` VALUES (5, '555', 'xy');
INSERT INTO `a_table` VALUES (6, '666', 'xy');

CREATE TABLE `b_table` (
`id` VARCHAR( 11 ) NOT NULL ,
`phone` VARCHAR( 20 ) NOT NULL ,
`message` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

 

I want to SELECT phone FROM a_table WHERE group =xy. and insert a string = "I LOVE YOU" to b_table(phone,message).

 

condition row of b_table After Query/ Insert is:

----+----------------+----------------------+
id  | phone          |  message             |
----+----------------+----------------------+
1   | 444            |  I LOVE YOU          |
----+----------------+----------------------+
2   | 555            |  I LOVE YOU          |
----+----------------+----------------------+
3   | 666            |  I LOVE YOU          |
----+----------------+----------------------+

 

how to create query  for this problem.

Thank you.

Link to comment
Share on other sites

but, there is no field message in the  a_table, 'I LOVE YOU' is a string.

$message="I LOVE YOU";

 

And who said we are selecting a "field" called `I LOVE YOU`? Did you notice that it is defined in that query as a String enclosed by single strait quote marks 'I LOVE YOU' and not a field with back ticks `I LOVE YOU`. Did you even take the 5 seconds needed to try the perfectly valid solution I provided you? No, because if you had you would have seen that it will work. It is perfectly valid to SELECT other things than fields. You can select static values, expressions, date/time, etc. etc. The MySQL manual is littered with such examples.

mysql> SELECT 1 + 1;
        -> 2

Link to comment
Share on other sites

gacoksa, did you even TRY what I provided you? It will do what you want - you just don't understand enough about SQL to know that it will and you are dismissing it out of hand. Not only is double posting a waste of time so is not trying the solutions provided. The code you are trying to use is worthless. You don't need a while loop. The format of the query I provided will work.

 

This is ALL you need

include ("koneksi.php");
$con = mysql_connect($servername,$username ,$password);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("smsd");
$group = mysql_real_escape_string(trim($_POST['group']));
$message = mysql_real_escape_string(trim($_POST['message']));

$balas_sms = "INSERT INTO outbox (phone_number, text)
              SELECT phone, '$message'
              FROM contacts
              WHERE group_name = '$group'";
$hasil_balas_sms = mysql_query($balas_sms);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.