ShaolinF Posted June 16, 2008 Share Posted June 16, 2008 Hi Guys Im trying to carry out an SQL statement using object oriented methods but am having some problems. See code below: $field = $_GET['statment']; /*Specifies table name*/ $db->query("INSERT INTO $wpdb->$field ($query)VALUES ($query_data)"); The problem is with the $wpdb->$field - It doesn't work when I use the $field var. If I use a table name like posts in its place then the code will work. But what I want to do is specify the table name in the GET method and feed it into the $field variable. That way, I won't need to make several queries with the different tables to do this. Does anyone know how I can do this? Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/ Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 <?php $db->query("INSERT INTO ".$wpdb->$field." (".$query.")VALUES ('".$query_data."')"); ?> The variables need to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/#findComment-566645 Share on other sites More sharing options...
ShaolinF Posted June 16, 2008 Author Share Posted June 16, 2008 I tried that and it gave me an unexpected T_OBJECT_OPERATOR error. Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/#findComment-566662 Share on other sites More sharing options...
ShaolinF Posted June 16, 2008 Author Share Posted June 16, 2008 Never mind. I managed to fix it. I am getting a syntax error when trying to run an update query: query("UPDATE " . $db->$field1 . "SET userlog = test WHERE ID = 4"); Anyone know where I could be going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/#findComment-566702 Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 query("UPDATE " . $db->$field1 . "SET userlog = test WHERE ID = 4"); Is $db->$field1 a field or table? There is no space between the $db->$field1 quotation and SET. Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/#findComment-566707 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 $db->field1 Quote Link to comment https://forums.phpfreaks.com/topic/110446-solved-table-names-for-queries/#findComment-566719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.