Jump to content

mdmartiny

Members
  • Posts

    188
  • Joined

  • Last visited

Posts posted by mdmartiny

  1. Hello my fellow PHP developers. I am writing a script that will allow for someone to create a table in their database but just filling in a few text boxes.

     

    I am coming across the problem Notice: Undefined offset: 1. I know what it means and all but I just do not know how to fix it. So if someone would be so kind to help me I would appreciate it.

     

    <?php 
    
    $table_name = $_POST['table_name'];
    
    $create_sql = "create table $table_name (";
    
    for ($i = 0; $i < count($_POST['field_name']); $i++) {
        $create_sql .= $_POST['field_name'][$i] . " " . $_POST['field_type'][$i];
    
    if($_POST['auto_increment'][$i] == "y" ) {
    	$additional = "NOT NULL auto_increment";
    } else {
    	$additional = "";
    }
    
    if($_POST['primary'][$i] == "y"){
    	$additional .= ", primary key(".$_POST['field_name'][$i].")";
    } else {
    	$additional = "";
    }
    
        if ($_POST['field_length'][$i] != "") {
            $create_sql .= "(" . $_POST['field_length'][$i] . ") $additional,";
        }
        else {
            $create_sql .= " $additional ,";
        }
    }
    $create_sql = substr($create_sql, 0, -1);
    $create_sql .= ")";
    
    $create_res = mysql_query($create_sql);
    
    if ($create_res) {
    
        $sql_column = "SHOW COLUMNS FROM $table_name";
        $res_column = mysql_query($sql_column);
    
        echo "<h3>the following database has been created <strong>". $table_name. "</strong></h3>";
            if (mysql_num_rows($res_column)) {
                echo "<table class='db-table' cellpadding='5' cellspacing='5'>";
                echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>";
                while ($row = mysql_fetch_row($res_column)) {
                    echo "<tr>";
                    foreach ($row as $key => $value) {
                        echo "<td>". $value ."</td>";
                    }
                    echo "</tr>";
                }
                echo "</table><br />";
            }
    
            echo "<a href='create_table.php'>Create</a> another table";
    }
    else {
        echo "<h3>There was an error" . mysql_error()."</h3>";
    }
    ?>

  2. Hello Everyone,

     

    I have created a form for a personal project that I am working on. I have some error checking and stuff in the form but what I would like to happen is when an error has occurred for the page to reopen the form with the error message. I know that this can be done with the header function. But how do I get the form to display the error.

     

    Here is the block of code that I am working with

    if ($file_error > 0) {
                        header ('Location: add_category.php');
                        echo "There was an error uploading file.";
                    }
                    else {
                        if ($file_type != $image_array) {
                            header ('Location: add_category.php');
                            echo "Invalid Image Type";
                        }
                        if ($file_name == FALSE) {
                            header ('Location: add_category.php');
                            echo "Please choose a image for your category";
                        }
                        if ($file_size > 5000000) {
                            header ('Location: add_category.php');
                            echo "File size is to large. Images must be smaller than 5MB.";
                        }
    
                        if (str_word_count($description) > 300) {
                            header ('Location: add_category.php');
                            echo "<p>category description must be less than 300 words<br />You have '.str_word_count($description).' words</p>";
                        }

    This is the form code that I am working with

    <div id="content">
                    <form id="add_cat_form" name="add_cat_form" action="do_category.php" method="post" enctype="multipart/form-data">
                        <fieldset>
                            <legend><p>Add Category</p></legend>
                            <p>
                                <label for="cat_name">Category Name:</label> <input class="input_box" type="text" name="cat_name" id="cat_name" />
                            </p>
                            <p>
                                <label for="cat_image">Category Image:</label> <input class="file" type="file" name="cat_image" id="cat_image"  />
                            </p>
                            <p>
                                <label for="cat_desc">Description:</label><br /><textarea class="cat_desc" name="cat_desc" id="cat_desc"></textarea><br />
                                <span id="word_count">300 words Max</span>
                            </p>
                            <input class="submit_button" type="submit" value="Add Category" />
                        </fieldset>
                    </form>
                </div>

  3. function count_words($description){

    $word_count = 0;

                    $punctuation = array('.', ',', '!', '?', ':', ';', '(', ')');

            $description = str_replace($punctuation, '', $description);

    $description = trim(preg_replace("/\s+/"," ",$description));

    $word_array = explode(" ", $description);

     

    for($i=0; $i < count($word_array); $i++){

    if (preg_match("[0-9A-Za-z]", $word_array[$i])) {

    $word_count++;

    }

    return $word_count;

    }

      }

  4. I am writing a function to count the words that have been typed into a text box. I originally had it working when I first wrote it but it was counting all of the punctuation as a word so I I tried adding some code to eliminate that. Now it is not working. Could someone help me?

     

    Here is my code

    function count_words($description){
    	 $word_count = 0;
    	 $description = trim(preg_replace("/\s+/"," ",$description));
    	 $word_array = explode(" ", $description);
    
    	 for($i=0; $i < count($word_array); $i++){
    		if (preg_match("[0-9A-Za-z]", $word_array[$i])) {
    			$word_count++;
    		}
    		return $word_count;
    	}
      }

  5. Thanks for all of the ideas. I have been trying to get some work from local businesses and such. Just trying to start somewhere.

     

    I have sent out letters and cold calling people. So far nothing so I think I am going to expand my area of looking and see what I can come up with.

  6. I am trying to write the code to do what I want but nothing I am doing is working. Could someone please give me a hand with it.

     

    This is what I have written so far.

     

    <p>
                            <label for="category">Category:</label><select name="category" id="category">
                                <option selected="selected" disabled="disabled" value="Select a Category">Select a Category......</option>
                                <?php
                                $select_sql = "SELECT * FROM category ORDER BY cat_id";
                                $cat_select = mysql_query($select_sql);
    
                                while ($row = mysql_fetch_assoc($cat_select)) {
                                    echo "<option value=\"" . $row['cat_id'] . "\">" . $row['category'] . "</option>\n  ";
                                }
                                ?>
                            </select>
                        </p>
                        <script>
    					var category = $('#category').val();
    					var cat_id =$('#category option:selected').data();
    
    					$('#make').change(function(){
    						$.post ('includes/sub_functions.php', {category:category, cat_id:cat_id}, function(data) {
    							$('#sub_category').html(data)
    						});
    					});
    				</script>
                        <p>
                            <label for='sub_category'>Sub Category:</label><select name='sub_category' id='sub_category'>
                                <option selected="selected" value='Select a Sub Category'>Select a Sub Category......</option>
                            </select>
                        </p>
    

     

    This is the function page

    <?php
    include('connection.php');
    connect();
    
    if(isset($_POST['category'])){
    $cat_id = mysql_real_escape_string($_POST['cat_id']);
    
    $sub_sql = "Select * from sub_cat, category where sub_cat.cat_id = category.$cat_id";
        $sub_select = mysql_query($sub_sql);
    
    while ($row = mysql_fetch_assoc($sub_select)) {
             echo "<option value=\"" . $row['sub_cat_id'] . "\">" . $row['sub_cat'] . "</option>\n  ";
                                }
    }
    ?>
    

  7. Hello Everyone,

     

    I have a select box with my main categories in it. Depending on what is selected by the user in the selection box another selection box will pop up with sub categories.

     

    I am not sure how to get it to open the subcategory selection box. I do not want the selection box with the sub categories in it showing at first. It would be after something has been chosen in the category selection box.

  8. Hello Everyone,

     

    I have written some code for my version of a like button. So visitors can "like" pages in my website. What I did was use a submit button to make the like button. But just like other submit buttons when you click the page refreshes and it throws of the page view counter.

     

    What I am looking to do is use Jquery and Ajax to get the likes from the database and place them on the page without refreshing the page. Since I am a complete newbie when it come to them. I do not know where to begin or to even look. I am hoping that someone can point me in the right direction so that I may start learning how to use Ajax and Jquery.

     

    <?php
    
    function like_add() {
        $last = $_GET['l'];
        $first = $_GET['f'];
        $ip = $_SERVER['REMOTE_ADDR'];
    
        $like_sql = 'Select * from `counter` where first = "' . $first . '" and last = "' . $last . '"';
        $like_result = mysql_query($like_sql);
        $like_row = mysql_fetch_assoc($like_result);
        $like = $like_row['likes'];
    
        echo "Likes $like";
        echo '<form name="like_add_form" action="" method="POST" class="like_add">
    <input type="submit" name="like_add" class="like_add_button" value="Like Me" />
    </form>';
    
        if (isset($_POST['like_add'])) {
            $page_id_sql = "SELECT `page_id` FROM `counter` WHERE first = '$first' AND last = '$last'";
            $page_id_result = mysql_query($page_id_sql);
            $page_id_row = mysql_fetch_assoc($page_id_result);
            $page_id = $page_id_row['page_id'];
    
            $voted_sql = "Select * FROM `liked_ip` where ip ='$ip' AND page_id='$page_id'";
            $voted_result = mysql_query($voted_sql);
            $voted_num_rows = mysql_num_rows($voted_result);
    
            if ($voted_num_rows != 0) {
                echo '<div class="error">You have all ready liked this signature</div>';
            }
            else {
                mysql_query("INSERT into `liked_ip` (id,page_id,ip) VALUES ('','$page_id','$ip')") or die(mysql_error());
                mysql_query("UPDATE counter SET `likes` = `likes` + 1 WHERE first = '$first' AND last = '$last'") or die(mysql_error());
            }
        }
    }
    
    ?>
    

  9. Let's see if I hot this right. From what I read from  foing some searching. I run the query and it its everything in a array. Using the mysql_fetch_assoc makes it an associative one. Will that work if I only want info from one row on table

  10. I am in the process of creating a like system for a personal website that I am working on. I am trying to pull information from one database and use that to place info into another database. When I do I get resource error #15.

     

    This is my code that I have so far

     

    function like_add() {
        $last = $_GET['l'];
        $first = $_GET['f'];
        $ip = $_SERVER['REMOTE_ADDR'];
    
        $like_sql = "Select * from `counter` where first = ' $first ' and last = '$last '";
        $like_result = mysql_query($like_sql);
    
        $likes = '<p class="like_button"> Likes ';
        while ($like_row = mysql_fetch_assoc($like_result)) {
            $like = $like_row['likes'];
    
            $likes .= " $like";
        }
        $likes .= '</p>';
        echo "$likes";
        echo '<form name="like_add_form" action="" method="POST" class="like_add">
    <input type="submit" name="like_add" class="like_add_button" value="Like Me" />
    </form>';
    
        if (isset($_POST['like_add'])) {
            $page_id = "SELECT `page_id` FROM `counter` WHERE first = '$first' AND last = '$last'";
            $page_id_result = mysql_query($page_id);
            echo $page_id_result; <----- Here I am trying to pull information from a field in my database to use as a variable and to place in the second database
    
            $voted = mysql_query("Select * FROM `liked_ip` where ip ='$ip' AND page_id='$page_id_result'"); <----- Here I want to use the information from the first table to place in the second one
    
            if (mysql_num_rows($voted) != 0) {
                echo "You have all ready liked this post";
            } else {
                mysql_query("INSERT into `liked_ip`(id,page_id,ip) VALUES ('$page_id_result','$ip')");
            }
            if (mysql_num_rows($voted) == 0) {
                mysql_query("UPDATE counter SET `likes` = `likes` + 1 where first = '$first' AND last = '$last'");
            }
        }
    }
    

     

    I know that I have to be doing something so simple that I am just not seeing it this late at night. I may even be completely wrong in all of this. If someone could please help me out I would appreciate it very much.

  11. I created a search function for a website that I am working on. Right now it is only searching one table. I would like to make it so it searches two tables.

     

    I am trying to do this the simplest way possible. So as not to clutter up my code anymore than I have two. The way that I have been trying to get it to do it is

    Select * from ttmautos, inperson_autos Where inperson_autos.l_name = 'something' or ttmautos.l_name = 'something'

     

    When I use this code I get repeated rows from the table. When I was searching online to see if I could figure out what the problem was and most things I read always used AND.

     

    The information does not need to be in both tables. When they are using the search feature of the site. The information could be in either table or in could pull from both tables.

     

    I have read about Joins. I am not sure if I am to new to MySql to understand them. I have also read about unions and what I understand is that both tables have to have the same number of columns and same type of field data. Both of my tables do not have the same number of columns.

     

    I would appreciate any help on this issue.

  12. I think I may of found where the problem is in the code but not sure of what I am doing wrong.

     

    When I echo out the $results query.

     

    $where = "";
    
    $keywords  = preg_split('/[\s+]/', $keywords);
    $total = count($keywords );
    
    foreach($keywords as $key=>$keyword) {
    	$where .= "`l_name` LIKE '%$keyword%'";
    	if($key != ($total - 1)){
    		$where .= "OR";
    	}
    }
    
    $results  = "SELECT * FROM `ttmautos` WHERE $where";
            echo $results;
    

     

    This is what I am getting

    SELECT * FROM `ttmautos` WHERE `l_name` LIKE '%%'

     

  13. I am trying to write a script that will allow me visitors to my site to search my database for items. I followed a tutorial that I had found and tweaked it to fit my needs. I am not getting any results it keeps telling that there was no search results found. Even though I have the items that I have been testing on in my database.

     

    I have two files the main search.php file and the search_func.php file

     

    Here is the search.php file

    <form method='post' action='search.php'>
    <fieldset>
    <input type='text' name='search' id='search' value='type name here'/>
    <input type='submit' name='search_submit' id='search_submit' value='search'/>
    </fieldset>
    </form>
    
    <?php
    
    include('includes/search_func.php');
    
    if(isset($_POST['search'])) {
    $keywords = mysql_real_escape_string(htmlentities(trim($_POST['search'])));
    
    $errors = array ();
    
    if (empty($keywords )){
    $errors[] = 'Please enter a search term';
    } else if (strlen($keywords ) < 3 ) {
    $errors[] = 'Search term must be more then three characters long';
    } else if (search_results($keywords  == false)) {
    $errors[] = 'Your search for ' .$keywords . ' showed no results';
    }
    
    if (empty($errors)) {
    $results = search_results($keywords );
    $results_num = count($results);
    $suffix = ($results_num != 1) ? 's' : '';
    echo 'Your search for <strong>'. $keywords  .'</strong> returned <strong>' .$results_num.'</strong> result'.$suffix.'';
    
    foreach($results as $result) {
    	echo $result['first'];
    }
    
    } else {
    foreach($errors as $error){
    echo $error;
    }
    }
    
    }
    
    ?>

     

    Here is the search_func.php file

    <?php
    include('includes/config.php');
    connect();
    
    function search_results($keywords ) {
    $returned = array();
    $where = "";
    
    $keywords  = preg_split('/[\s+]/', $keywords );
    $total = count($keywords );
    
    foreach($keywords  as $key=>$keyword) {
    	$where .= "`l_name` LIKE '%$keyword%'";
    
    	if($key != ($total - 1)){
    		$where .= "OR";
    	}
    }
    
    $results  = "SELECT `l_name`,`f_name`,`image`,`item_return` FROM `ttmautos` WHERE $where";
    $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0 ;
    
    if ($results_num === 0) {
    	return false;
    } else {
    	while($results_row = mysql_fetch_assoc($results)) {
    		$returned_results[] = array(
    			'last' => $results_row['l_name'],
    			'first' => $results_row['f_name'],
    			'image' => $results_row['image'],
    			'item' => $results_row['item_return']
    		);
    	}
    
    	return $returned_results;
    }
    }
    ?>
    

     

    If anyone could give me an idea of what I am doing wrong I would appreciate it.

×
×
  • 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.