I have a problem with this script. I would like to have the user input used in the query.
I have been trying for days and numerous methods with no success.

<?php

function new_module_menu() {
  $items = array();
  $items['new_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'new_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function new_module_form() {
  return drupal_get_form('new_module_my_form');
}

function new_module_my_form($form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
  );
  
  // Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  
  return $form;
}
$q=$form['name'][0];

// Modifies this function so that it will respond appropriately based on
// which page was submitted. If the first page is being submitted,
// values in the 'storage' array are saved and the form gets 
// automatically reloaded.
// If page 2 was submitted, we display a message and redirect the 
// user to another page. 
function new_module_my_form_submit($form, &$form_state) {
  // Handle page 1 submissions
  if ($form_state['clicked_button']['#id'] == 'edit-next') {
    $form_state['storage']['page_two'] = TRUE; // We set this to determine
                                               // which elements to display
                                               // when the page reloads.
                                               
    // Values below in the $form_state['storage'] array are saved 
    // to carry forward to subsequent pages in the form.
    $form_state['storage']['page_one_values'] = $form_state['values'];
  }
  // Handle page 2 submissions
  else {
    // Connects to your Database 
mysql_connect("localhost","drupaluser","sep221984")or die(mysql_error()); 
mysql_select_db("drupal") or die(mysql_error()); 
//$query = "SELECT `languagearts`.`Description`\n"
//    . "FROM languagearts\n"
//    . "WHERE ((`languagearts`.`Description` LIKE \"%$ke%\") AND (`languagearts`.`Grade` = 
//\"Grade 6\"))\n AND (`languagearts`.`Label` like \"p%\")\n"
//    . " ";
$query = "select * from languagearts where Description like \"%$q%\" order by Description";
//$query = "select * from languagearts where Description like '".$q."' order by Description";
$result =  db_query($query);
  while ($row = db_fetch_object($result)) {
  $Description="{$row->Description}";
  $url_pr=str_replace("http://","",$url);
  echo "<strong>".$org."</strong><br>\n";
    if($Description!=""){ echo $Description."<br>\n"; }
  echo "<br>\n";
  }
  while($a_list = db_fetch_object($result)){
  foreach ($a_list as $array => $a){
  }
print("<tr><td>".$a."</td></tr>\n");
}
	/* 
     Normally, some code would go here to alter the database with the data
     collected from the form. Sets a message with drupal_set_message()
     to validate working code.
     */
    drupal_set_message('Your form has been submitted');
    unset ($form_state['storage']); // This value must be unset for 
                                    // redirection! This is because
                                    // $form_state['rebuild'] gets set to TRUE
                                    // when 'storage' is set. See code sample
                                    // #9 for more on this.
                                    
   //$form_state['redirect'] = 'node/5'; // Redirects the user.
  }
}

Recommended Answers

All 5 Replies

Just a shot but try: $query = "SELECT * FROM `languagearts` WHERE `Description` LIKE '%".$q."%' ORDER BY Description"; If this is the right line, you never really specified which line you were having problems with.

Member Avatar for rajarajan2017
$query = "select * from languagearts where Description like \"%$q%\" order by Description";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

Find out the error what is shown by the above statement.

can you specify what user input you want?
is it from a link or from a search ect

This PHP script creates a text box in which a user types in a word to be used in the query. Then submits it to the query with a submit button.

dnirvine: It resulted in the same output-listing every row.
This PHP script creates a text box in which a user types in a word to be used in the query. Then submits it to the query with a submit button.
After trial and error I found it is my assignment to $q that is the problem.If i just assign: $q='read'; it displays read on a new page when I: echo $q;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.